Android Question Programmatic Page Navigation in B4XPages

doncx

Active Member
Licensed User
Longtime User
In a B4XPages project, I am stepping through a sequence of six pages forward and back.

I am not using the title bar or built-in navigation. I use buttons to move forward and back through the sequence. There are valid UI reasons for doing this.

I am developing in B4A and will add B4i later.

My problem is related to the page stack. With the "forward" button, I have been using B4XPages.ShowPage(<next page>) and with the back button I have been using B4XPages.ShowPageAndRemovePreviousPages(<previous page>).

This always works even though the standard B4XPages logging seems to show that the stack has been removed. The problem is that the phone's back button seems to lose track of the stack and when pressed *sometimes* it closes the app and returns the user to the phone home screen. Just sometimes (and I'm not sure why).

I can't use ShowPage() to go back because it adds to the stack.

Even though it works, I can't use ClosePage() because I'll be adding a B4i version in the near future and the documentation says in B4i it only works on the top page.

So, how should I programmatically move back through a sequence of pages in a manner that respects the stack, works on all platforms, and is compatible with the phone's back button so that it can be used for the same purpose?

Many thanks for any assistance.
 
Solution
What you are doing is reasonable. You just need to ignore the Back button.

You can do this by trapping the keypress event.

B4X:
Sub b4xpage_KeyPress (KeyCode As Int) As Boolean
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
            Return True  ' Throw awwy the back button
    End Select
    Return False
End Sub

or your could do it globally as shown here:


This is only relevant for Android.

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
What you are doing is reasonable. You just need to ignore the Back button.

You can do this by trapping the keypress event.

B4X:
Sub b4xpage_KeyPress (KeyCode As Int) As Boolean
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
            Return True  ' Throw awwy the back button
    End Select
    Return False
End Sub

or your could do it globally as shown here:


This is only relevant for Android.
 
Upvote 0
Solution

Spavlyuk

Active Member
Licensed User
B4XPages.ShowPageAndRemovePreviousPages clears the stack. Pressing the back button after that closes the app. Not sure what the issue with B4XPages.ClosePage is, aren't your navigation buttons working on the top page anyway?
 
Upvote 0

doncx

Active Member
Licensed User
Longtime User
Yes, that was my expectation, but the back button almost always continues to work (through the sequence of pages) after calls to ShowPageAndRemovePreviousPages. But not always.

The problem with ClosePage() is that though it works on Android, the documentation says it only works on the "top" page in B4i.
 
Upvote 0

Spavlyuk

Active Member
Licensed User
Yes, that was my expectation, but the back button almost always continues to work (through the sequence of pages) after calls to ShowPageAndRemovePreviousPages. But not always.
Are you manipulating the stack after calling ShowPageAndRemovePreviousPages? You can see in the three pages example, when you log in, ShowPageAndRemovePreviousPages is called and pressing back button after that closes the app.

it only works on the "top" page in B4i.
Yes, what I was asking is whether your back button works on a page that isn't on top, since I don't quite understand how it would work in that case.
 
Upvote 0
Top