Block Back Key in Msgbox

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I have in my keypress event a check for the Back key and a confirmation of Yes or No. If the ans = DialogResponse.NEGATIVE I return True and abort the back. I don't even have a False since it isn't needed like other examples here show with the full if/then/else.

While the MsgBox is showing though the Back key can still be pressed and the app exits without even answering the msgbox. Is there a way to block this? ProgressDialogShow2 has an option to ignore back, but I'm not seeing one in either msgbox...do we need a msgbox3?

The whole purpose of the box is to confirm an accidental press...which could occur twice in a row.
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
Even if you don't use CANCEL you can still trap it:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If KeyCode = KeyCodes.KEYCODE_BACK Then
    
       Selection = Msgbox2("Exit the app?", "Confirmation", "Yes", "", "No", Null)
       
       Select Selection
       
              Case DialogResponse.POSITIVE
              
                   Activity.Finish
                   
              Case DialogResponse.CANCEL
              
                   Return True
                   
              Case DialogResponse.NEGATIVE
              
                   Return True
       
       End Select
    
    End If
    
End Sub

Here you will have to answer either YES or NO to exit, pressing the BACK button will just continue running the app.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Figures it would be something that simple. Changed the If to:
If ans = DialogResponse.NEGATIVE or ans = DialogResponse.CANCEL Then Return True

Something to add to the Beginner's Tip Section- Cancel = Back

I might have to look at the actual values and improve that with some type of bit compare I always hate having an If with two ='s.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Hre is another way to skin NJDude's cat, very similar to his and avoids the double equals Roger is concerned about.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
      Dim Selection As Short
    If KeyCode = KeyCodes.KEYCODE_BACK Then    
       Selection = Msgbox2("Exit application?".ToUpperCase, "C o n f i r m a t i o n", "Yes", "", "No", Null)       
       Select Selection   
              Case DialogResponse.POSITIVE              
                   Activity.Finish                   
              Case DialogResponse.CANCEL, DialogResponse.NEGATIVE 
                   Return True                      
       End Select   
    End If   
End Sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Ok, one more way to skin that pesky cat:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If Msgbox2("Exit app?", "Exit", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then Activity.Finish Else Return True

End Sub

Or:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean

    If Msgbox2("Exit app?", "Exit", "Yes", "", "No", Null) = -1 Then Activity.Finish Else Return True

End Sub
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If Activity.Finish is of concern, you can replace it here with Return False. See below. NJ, you got more pesky cats to skin.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
      Dim Selection As Short
    If KeyCode = KeyCodes.KEYCODE_BACK Then    
       Selection = Msgbox2("Exit application?".ToUpperCase, "C o n f i r m a t i o n", "Yes", "", "No", Null)       
       Select Selection   
              Case DialogResponse.POSITIVE                                
                   Return False 
'                   Activity.Finish                   
              Case DialogResponse.CANCEL, DialogResponse.NEGATIVE 
                   Return True                      
       End Select   
    End If   
End Sub
 
Upvote 0

Gargoyle

Member
Licensed User
Longtime User
I've been trying this on my app, it displays the msgbox but then quits straight out and you don't have time to give a response.

Any suggestions or additions needed?

(P.S. I'm using B4A 2.02)

Thanks.
 
Upvote 0

Gargoyle

Member
Licensed User
Longtime User
I've been trying this on my app, it displays the msgbox but then quits straight out and you don't have time to give a response.

After a lot of :BangHead:, I managed to solve my problem...
(Additions/adaptations)
B4X:
Sub Process_Globals
   Dim exitdelay As Timer
   exitdelay.Initialize("exitdelay",10)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   'added to END of Sub - seems to be needed.
   If UserClosed Then
      Activity.Finish
   End If
End Sub

Sub Activity_KeyPress(KeyCode As Int) As Boolean
   If KeyCode = KeyCodes.KEYCODE_BACK Then
      exitdelay.Enabled = True
      Return True
   End If
End Sub

Sub exitdelay_tick
   exitdelay.Enabled = False
   If Msgbox2("Exit app?", "Exit", "Yes", "", "No", Null) = -1 Then
      Activity_Pause (True)
   End If
End Sub

Hope this helps someone else.
 
Upvote 0

mred1

Member
Licensed User
Longtime User
After a lot of :BangHead:, I managed to solve my problem...
(Additions/adaptations)
B4X:
Sub Process_Globals
   Dim exitdelay As Timer
   exitdelay.Initialize("exitdelay",10)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   'added to END of Sub - seems to be needed.
   If UserClosed Then
      Activity.Finish
   End If
End Sub

Sub Activity_KeyPress(KeyCode As Int) As Boolean
   If KeyCode = KeyCodes.KEYCODE_BACK Then
      exitdelay.Enabled = True
      Return True
   End If
End Sub

Sub exitdelay_tick
   exitdelay.Enabled = False
   If Msgbox2("Exit app?", "Exit", "Yes", "", "No", Null) = -1 Then
      Activity_Pause (True)
   End If
End Sub

Hope this helps someone else.
 
Upvote 0

mred1

Member
Licensed User
Longtime User
Thank you very much for this! Although I have been frying many other fish prior to deploying my app soon, this is actually all I was looking for (for quite sometime now), and saved it as my last issue on my dog fooding list. A simple way to determine if the end-user acidently depressed the back key (among a few other things). I believe that many others have been asking for this simple answer, and getting more complex answers than necessary. Thanks again.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Wrong thread.
You always should create a new thread for any new question.
Especially If the thread is four years old
 
Upvote 0

mred1

Member
Licensed User
Longtime User
Wrong thread.
You always should create a new thread for any new question.
Especially If the thread is four years old
It wasn't a question, but feedback from an old post from someone that gave me resolution while researching all (old and new) posts?
 
Upvote 0
Top