Android Question Wait for Response

epiCode

Active Member
Licensed User
I am calling a sub called ShowDialog from multiple places in code. Message1 is string Variable.

Intended outcome:
My code should display Dialog and wait for either ok or cancel.

Current scenario:
Dialog is shown but code moves on to Additional Code

Would be really nice if anyone can share or point out a more optimal way to reorganizing this to achieve intended outcome.

B4X:
Sub ShowDialog as Resumablesub
'Some code related to dialog
        dlg.Title = "Warning !"            'dlg is B4XDialog
        Dim sf As Object = dlg.Show(Message1, "OK", "", "CANCEL") 'Message1 is global string variable
        CliptoOutline(dlg.Base,10dip)
        Wait For (sf) Complete (Result As Int)
        Return Result
End Sub


Sub callingSub
' Some Code
...
    Wait For (ShowDialog) complete (result As Int)
        If result = xui.DialogResponse_Cancel Then
            'Do something
        Else
            'Do something else
        End If

' Additional Code Continues
 
Solution
I don't see why your code wouldn't work. I mocked-up an example of how I would do it.

B4X:
Private Sub Button1_Click
    Wait For (ShowDialog) complete (result As Int)
    If result = xui.DialogResponse_Cancel Then
        Log("Cancel")
    Else
        Log("OK")
    End If
   
    ' Additional Code Continues
    Log("This should not happen if I don't reply to the warning")
End Sub

Sub ShowDialog As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Delete file?", "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log("Deleted!!!")
    End If
    Return Result
End Sub

William Lancee

Well-Known Member
Licensed User
Longtime User
I don't see why your code wouldn't work. I mocked-up an example of how I would do it.

B4X:
Private Sub Button1_Click
    Wait For (ShowDialog) complete (result As Int)
    If result = xui.DialogResponse_Cancel Then
        Log("Cancel")
    Else
        Log("OK")
    End If
   
    ' Additional Code Continues
    Log("This should not happen if I don't reply to the warning")
End Sub

Sub ShowDialog As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Delete file?", "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log("Deleted!!!")
    End If
    Return Result
End Sub
 
Upvote 0
Solution

epiCode

Active Member
Licensed User
I don't see why your code wouldn't work. I mocked-up an example of how I would do it.
Checked your code in B4J - Works
Changed msgbox to dialog - Works
moved it to original project - Fails
It seems that some other ResumableSub/Event/Sleep is causing the code to assume/detect wait for complete trigger and proceed.
For now your Solution works in isolation hence marking it as solution!
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Remember that wait for and sleep pass control over to the calling program which will continue with its steps.
It also needs to wait. This has to be done right to the top of the calling chain.

My guess is that at some point a process continues without waiting for the user's response.

Sub A calls Sub B which calls the ShowDialog
B waits for the user's response, but if B is not a ResumableSub then A won't wait.
 
Upvote 1

epiCode

Active Member
Licensed User
Remember that wait for and sleep pass control over to the calling program which will continue with its steps.
It also needs to wait. This has to be done right to the top of the calling chain.

My guess is that at some point a process continues without waiting for the user's response.

Sub A calls Sub B which calls the ShowDialog
B waits for the user's response, but if B is not a ResumableSub then A won't wait.
You guessed it right !
it was main thread calling Sub A which called Sub B calling SubDialog
So I had to make sub B a resumable Sub with return value and it is working now.
 
Upvote 0

Similar Threads

Top