Using the BACK button

latcc

Banned
I have a noobe question. If I use this code to trap a BACK keypress it will always present the Exit Program messagebox to the user...

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If Msgbox2("Exit Program?", "EXIT", "Yes", "", "No", Null) =DialogResponse.POSITIVE Then
             activity.finish
        Else
             Return True
        End If
    End If
End Sub


Problem:

The BACK button is also used to withdraw out of child displays, like a menu or help panel. When the above code is used, while at a child level, the effect is to ask the user if they wish to exit the program, when the correct process is to close the child display and return to the base level of the program.

How do we allow the BACK button to withdraw from child displays but STILL be able to ask the user 'exit program' when at the base parent level of the program? Because the above routine ALWAYS intercepts the BACK button whatever program level is displayed.

I tried using a boolean variable that kept track of whether the program was at the base level or in a child display level. I added:

B4X:
If PROGRAMBASE = False Then Return True  'not at the base level so don't ask about exiting

to the start of the above routine. This simply stopped the BACK button from working in its normal backing-out process when not at the base parent level.

It seems you can not have the BACK button perform both functions: for backing out from a child display and for a friendly exit messagebox.

Any ideas?



[Note on the code: I used Erel's code but found it didn't work so I changed his 'Return False part to 'activity.finish', and now the messagebox part works.]
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
You will have to change the value of your flag when you open the child window and reset it when you close it, something like:

B4X:
In pseudo-code:

If OpenChild then Flag = true

If BackKey pressed and Flag = True then 
   Flag = False
   CloseChild

If BackKey pressed and Flag = False then
   Exit App

I hope that makes sense. :)
 
Upvote 0

latcc

Banned
You will have to change the value of your flag when you open the child window and reset it when you close it, something like:

B4X:
In pseudo-code:

If OpenChild then Flag = true

If BackKey pressed and Flag = True then 
   Flag = False
   CloseChild

If BackKey pressed and Flag = False then
   Exit App

I hope that makes sense. :)

Yes that is how my little test app works too. But it still doesn't answer how to make the child window actually [close]. Your 'CloseChild' text is where the unknown code will be that I need. But what is that code?? Unless you are saying 'CloseChild' is the keyword or function that will actually [close] a child window, then we are still no nearer an answer. :)

As I understand it the process that would normally close down the child display process is being blocked by the 'Exit App' messagebox code. How to do both like normal Android commercial apps can do???
 
Last edited:
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
For example if you child process is on a panel (like you mentioned a help panel) you could use something like this:


B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
      If pnlHelp.Visible = True Then
         pnlHelp.Visible=False
         Return True
      End If
    Else
        If Msgbox2("Exit Program?", "EXIT", "Yes", "", "No", Null) =DialogResponse.POSITIVE Then
             activity.finish
        End If
    End If
End Sub

This would close the panel but not ask to exit the program. For more child processes in panels or different activities you should use a global var like NJDude suggested.

Rolf
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I tend to handle it slightly differently and deal with multiple popups, if a pop up is closed then true is returned to the Activity_Keypress routine and it then exits setting the return value to true so nothing else happens.

If you load all your layouts at the beginning of the app you don't need to check that the panel is initialized in Close Open Popups.

To close the window you just set it's visible parameter to false.

B4X:
Sub Activity_Keypress(KeyCode As Int) As Boolean

   If KeyCode = KeyCodes.KEYCODE_BACK Then
   
      'Check for and Manage Popups And Menus
      Found=CloseOpenPopUps
      If Found Then Return True

                'Your exit app routine goes here

End Sub

Sub CloseOpenPopups As Boolean
   'Manage Popups and Menus
   
   If Panel1.IsInitialized And Panel1.Visible = True Then
      Panel1.Visible=False
      Return True
   End If

        'More Popups go here

        Return False
End Sub
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
Stevel05's code works well, but requires two backkey presses to close the activity if popups are not open. I didn't understand why
 
Upvote 0
Top