Android Question Captuting Androids back button - consistantly

DKnowles

Member
Licensed User
Longtime User
Hi All

I'm tring to use the androids back button to move back to the B4XPages page that called the current page.
The app works fine sometimes and the back button closes the page and reviels to page above, BUT sometimes it minimises the the whole app, sometimes nothing.

I have just added this, but it is not triggered when the android phones main back button is clicked, not we have the title bar hidden so the b4xpages X is not in the screen.

If someone could indicate how to change the background colour of the activity title bar we could use that, but it needs to change depending on the user that is logged in.

Cheers in advance David

Example kindly taken from b4x.com:
Private Sub On_KeyPress(ViewTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
 
        Dim j As JavaObject = KeyEvent
        Dim eventCode As Int = j.RunMethod("getAction",Null)
        Log("EventCode is:"&eventCode)
        If (eventCode = 0) Then
            ' 0=ACTION_DOWN, 1=ACTION_UP, 2=ACTION_MULTIPLE
            Main.MonitoringBackButtonPressed = True ' This is just to let the MonitoringListTypes page know that the user pressed then back button on its child page, our code does different things depending if you are coming down to the page or working you way back up towards to home page.
            B4XPages.ShowPageAndRemovePreviousPages("MonitoringListTypes")
        End If
        Return True

    Else

        Return False

    End If
End Sub
 

JohnC

Expert
Licensed User
Longtime User
Why are you using Javaobject?

Maybe try not using it:
B4X:
Private Sub On_KeyPress(ViewTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
 
        Main.MonitoringBackButtonPressed = True ' This is just to let the MonitoringListTypes page know that the user pressed then back button on its child page, our code does different things depending if you are coming down to the page or working you way back up towards to home page.
        B4XPages.ShowPageAndRemovePreviousPages("MonitoringListTypes")
        Return True

    Else

        Return False

    End If
End Sub
 
Upvote 0

DKnowles

Member
Licensed User
Longtime User
Just in case someone else made the same silly mistake as me.....

I had acidently used a B4XPages.ShowPageAndRemovePreviousPages(targetPage) to close a child page at the bottom of the pages stack and manually go to its parent page (or via logic to home page direct), so no stack existed so the app closed.

Thanks again, all working now :)
 
Upvote 0
Top