Android Question Return result from a Class to Activity

demasi

Active Member
Licensed User
Longtime User
I want to create a class for my dialogs, but I don't know how to get the result of my dialog, from the class to the activity like in this code, from the dialogs lib demo. As you can see, the result is in the same sub, like a return from a function:

B4X:
Sub btnInput_Click
    Dim Id As InputDialog
    Id.Input = ""
    Id.Hint = "Enter some text!"
    Id.HintColor = Colors.ARGB(196, 255, 140, 0)
    ret = DialogResponse.CANCEL
    ret = Id.Show("Input the required text", "B4A Input Dialog", "Yes", "No", "Maybe", Bmp)
    ToastMessageShow(ret & " : " & Id.Input, False) 
End Sub

I don't know how to code this in a class.Can someone give-me a clue?

I can get a result like this using this code, not recommended as it stops the thread at the while loop. But it does what I need. Wrong way.

B4X:
public Sub show
    pnl.Initialize("pnl")
    pnl.Color=Colors.White
    ed.Initialize("")
    ed.TextColor=Colors.Black
    Dim b1 As Button
    b1.Initialize("b1")
    pnl.AddView(ed,10dip,10dip,200dip,60dip)
    pnl.AddView(b1,10dip,100dip,100dip,50dip)
    mActivity.AddView(pnl,5%x,25%x,90%x,50%x)
    btt=0 'this is defined at class globals
    Do While btt=0
        DoEvents
    Loop
    Return ed.Text
End Sub

Sub b1_click
    Dim w As Button
    w=Sender
    ret = ed.Text
    pnl.RemoveView
    btt=1
End Sub

If I try to code the correct way, without blocking the thread, it would like this:

B4X:
public Sub show
    pnl.Initialize("pnl")
    pnl.Color=Colors.White
    ed.Initialize("")
    ed.TextColor=Colors.Black
    Dim b1 As Button
    b1.Initialize("b1")
    pnl.AddView(ed,10dip,10dip,200dip,60dip)
    pnl.AddView(b1,10dip,100dip,100dip,50dip)
    mActivity.AddView(pnl,5%x,25%x,90%x,50%x)
    btt=0 'this is defined at class globals
End Sub

Sub b1_click
   ret = ed.Text
   pnl.RemoveView
   CallSub(Main,"Message")
End Sub

"Message" is a sub at the Main activity:

B4X:
...
Dim class as MyClass
...
class.initialize()
...

Sub Message()
    Log(class.ret)
End Sub

This way it works, but the program flow is more complicated, because the return is in another sub.
I want to use like in the Dialogs lib, or my example above, with the loop, where the return of the class is is the same sub, top down.
How can I make this?

Thank you for any help.
 

demasi

Active Member
Licensed User
Longtime User
thank you, Erel. I know that.
But how they made in dialogs library, like the example above, to return in the same subroutine?

B4X:
ret = Id.Show("Input the required text", "B4A Input Dialog", "Yes", "No", "Maybe", Bmp)
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
Thank you, Erel.
When I have some kind of working and polished result I will post here to help others.
 
Upvote 0
Top