Back Button and Modal MSg2

padvou

Active Member
Licensed User
Longtime User
Dear all,
using this:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK Then

      Dim result As Int
    
   result = Msgbox2("msg", "title", "yes","cancel","no",  Null)
      If result=DialogResponse.NEGATIVE Then
         ToastMessageShow("do not exit",False)
         Return True
      Else If result=DialogResponse.positive Then
ToastMessageShow(" exit",False)
how could the messagebox2 block the back button to be pressed again and override the messagebox itself?
 

padvou

Active Member
Licensed User
Longtime User
:sign0163:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
   
       Dim result As Int
       result = Msgbox2 ("App Exit?","","Yes","","No",Null)
          If result=DialogResponse.POSITIVE Then
            Activity.finish
         Else If result=-3 Then      'Add this to recapture back button pressed while MsgBox is shown
            Return True
          Else
              Return True
            
         End If
   End If
   
End Sub


What to you think?
 
Last edited:
Upvote 0

padvou

Active Member
Licensed User
Longtime User
The back button will always close the modal dialog. You can however show the dialog again if cancel was pressed (or the back button).

Yes it will. But this way if i don't for example to exit an activity by means of the back button, pressing back will show a messagebox, pressing it again will make the messagebox close, pressing back once more will make the messagebox re-appear and so on...
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
:sign0163:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
    
        Dim result As Int
        result = Msgbox2 ("App Exit?","","Yes","","No",Null)
            If result=DialogResponse.POSITIVE Then
                Activity.finish
            Else If result=-3 Then        'Add this to recapture back button pressed while MsgBox is shown
                Return True
            Else
                Return True
                
            End If
    End If
    
End Sub
What to you think?

I think that the 'else if result=-3' is not needed, since you are consuming the 'cancel' keystroke. Of course I may be missing something here, in means of what you actually want to achieve.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Ok, I've read more carefully your post and Erel's reply. Is this, perhaps, what you want to achieve?
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
        showExitDialogBox
        Return True
End If
    
End Sub

Sub showExitDialogBox
    Dim result As Int
    result = Msgbox2 ("App Exit?","","Yes","","No",Null)
    Select result
    Case DialogResponse.POSITIVE 
        Activity.Finish 
    Case DialogResponse.CANCEL 
        showExitDialogBox
    End Select
            
End Sub
 
Upvote 0
Top