Android Question B4A 7.00 MsgBoxAsync - how?

GuyBooth

Active Member
Licensed User
Longtime User
I have translated many of my MsgBox2 instances to MsgBox2Async with no problems, but I have a few where I am running into the restriction of not being able to use it in a Sub that returns a result. Maybe someone can point me to a way of handling this. Here is a simple example:

In a service, I call the activity to run a messagebox:
B4X:
' In the service module:
bFadeStopClearTracks = CallSub2(TMM_Run, "Confirm", "Empty the Queue?")
I am looking for the result to be returned. My code with MsgBox2 was:
B4X:
' In the activity module TMM_Run:
' Returns True if "Yes" is clicked, False if "No" is clicked
Sub Confirm(Message As String) As Boolean
    Dim bResult As Boolean
    bResult = (Msgbox2(Message,"Music Machine","Yes","No","", _
                TMM.gbmpLogo36x36) = DialogResponse.POSITIVE)
    Return bResult
End Sub
I want to use this
B4X:
Sub Confirm(Message As String) as Boolean
    Msgbox2Async(Message, "Music Machine", "Yes", "No", "", _
        TMM.gbmpLogo36x36, False)
    Wait For MsgBox_Result (iResult As Int) 
    Return (iResult = DialogResponse.POSITIVE)
End Sub
But using MsgBox2Async, return values are not allowed from the Sub "Confirm".
Is there any way to work around this? Can Sender somehow be used to call back to the Sub in the service?
Or is there some way to see the iResult from within the Calling Sub in the service?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the Activity code to:
B4X:
Public Sub Confirm(Callback As Object, Message As String)
   Msgbox2Async(Message, "Music Machine", "Yes", "No", "", Null, False)
   Wait For MsgBox_Result (iResult As Int)
   CallSubDelayed2(Callback, "Confirm_Result", iResult = DialogResponse.POSITIVE)
End Sub

Call it from the service with:
B4X:
CallSub3(Main, "Confirm", Me, "Delete?")
Wait For Confirm_Result(Result As Boolean)
Log(Result)
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Change the Activity code to:
B4X:
Public Sub Confirm(Callback As Object, Message As String)
   Msgbox2Async(Message, "Music Machine", "Yes", "No", "", Null, False)
   Wait For MsgBox_Result (iResult As Int)
   CallSubDelayed2(Callback, "Confirm_Result", iResult = DialogResponse.POSITIVE)
End Sub

Call it from the service with:
B4X:
CallSub3(Main, "Confirm", Me, "Delete?")
Wait For Confirm_Result(Result As Boolean)
Log(Result)
I have been able to change all except two instances to use this approach (not always quite this straightforward but I have found ways to get there)
However I have two instances where I don't know how to make it work - both in Activity_Keypress, where I am asking whether the user really wants to quit...
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    'return true to consume the event
    '"True" will stop it from closing the activity.
    Dim bResult As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If pnlClickShield.Enabled Then
            DeactivateClickShield
            ' Consume the event
            bResult = True
        Else
            ' Has the configuration changed?
            ' If so, ask whether the new configuration should be used
            If bConnConfigurationChanged Then
                iExitState = Msgbox2("The Configuration has changed." & CRLF & _
                                    "Do you want to use the new configuration?","The Music Machine", _
                                    "Use","Cancel","Abandon",TMM.gbmplogo36x36)
                If iExitState = DialogResponse.CANCEL Then
                    bResult = True
                Else if iExitState = DialogResponse.NEGATIVE Then
                    bResult = False               
                Else   
                    TMM.gbSettingsChanged = True
                    bResult = False
                End If
            End If
        End If
    End If  
    Return bResult
End Sub

In this specific case, the iExitState is also used by another activity after this one shuts down, but it is the need to return the bResult that is causing me grief.
Obviously the MsgBox2Async would require a callback - but where to? Maybe there's another approach that I've never come across?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If KeyCode = KeyCodes.KEYCODE_BACK Then
     HandleBackKey   
     Return True   
   End If
   Return False
End Sub

Private Sub HandleBackKey
   Confirm(Me, "Close?")
   Wait For Confirm_Result(Result As Boolean)
   If Result Then Activity.Finish
End Sub

Public Sub Confirm(Callback As Object, Message As String)
   Msgbox2Async(Message, "Music Machine", "Yes", "No", "", Null, False)
   Wait For MsgBox_Result (iResult As Int)
   CallSubDelayed2(Callback, "Confirm_Result", iResult = DialogResponse.POSITIVE)
End Sub

BTW, what you don't see in that code is the that B4A framework needs to treat specially the back key in order to allow developers to show a modal dialog at that point. If you try to show a modal dialog as a result of a different key then it will crash. The code posted here will work with any key.
 
Upvote 0
Top