Android Question Back button disappearing while navigating between pages.

beelze69

Active Member
Licensed User
Longtime User
Hi,

I have a b4X application with 3 Pages..
viz. 'Main Menu Page' -> 'Page 1' ->'Page 2'

So, I am calling 'Page 2' from 'Page 1' via B4xPages.ShowPage...

In 'Page 2' , I have disabled the BACK BUTTON by using 'Return False' in the B4XPage_CloseRequest event which I have written under 'Page 2'.. i.e. The BACK BUTTON is visible in 'Page 2' but nothing happens if I select it (which is what I want)..


Now, in 'Page 2' I have 2 buttons

Button 1-> For confirming some tasks and returning to 'Page 1'

Button 2-> For cancelling everything and returning to 'Page 1'

For both the the buttons I have used B4XPages.ShowPageAndRemovePreviousPages( "id of Page 1") in their respective 'Click' events.


Upon the start of the Program I can do the following:

1) I can go to 'Page 1' from my 'Main Menu Page' and use the BACK button on 'Page 1' and go back to 'Main Menu Page'.
Note that the 'Back Button' on 'Page 1' is VERY MUCH VISIBLE when I am going forward from 'Main Menu Page' to 'Page 1'.
2) From 'Page 1' I can go to 'Page 2'.

The Problem:

From 'Page 2', after clicking 'Button 1' or 'Button 2', When the control goes to 'Page 1' again, there is NO BACK BUTTON on 'Page 1' due to which I am unable to go to 'Main Menu Page' from there..

NOTE: If suppose, I take the control back to 'Main Menu Page' DIRECTLY from both Button 1 and Button 2 (of 'Page 2') and again invoke 'Page 1' from the 'Main Menu Page'.. then everything is fine ... But I want the control to go back to 'Page 1' from 'Page 2' and in 'Page 1' the end-user can either do other tasks or press the BACK BUTTON on 'Page 1' to go back to the 'Main Menu Page'.. Now this 'BACK BUTTON' is NOT VISIBLE when I take control back from 'Page 2' to 'Page 1' (as described above) and that is the issue...

I hope I have made it clear...

Requesting for help in this regard...

Thanks..
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
When the control goes to 'Page 1' again, there is NO BACK BUTTON on 'Page 1'
For both the the buttons I have used B4XPages.ShowPageAndRemovePreviousPages( "id of Page 1") in their respective 'Click' events.
The "back button" allows you to return to the calling page; if you remove that page, page 2 in your case, by calling ShowPageAndRemovePreviousPage, you cannot return to it, of course.

Note that, however, if page 2 calls page 1, the back button would return you to page 2, not to the MainPage, because the button takes you back to the calling page.

You probably need to add a home button on page 1 to "jump" to MainPage.
 
Last edited:
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
The "back button" allows you to return to the calling page; if you remove that page, page 2 in your case, by calling ShowPageAndRemovePreviousPage, you cannot return to it, of course.

Note that, however, if page 2 calls page 1, the back button would return you to page 2, not to the MainPage, because the button takes you back to the calling page.

You probably need to add a home button on page 1 to "jump" to MainPage.
Hi Lucas,

Thanks for the response.

I find this weird ...

I personally feel that the end-user should be able to 'Go back the way they have ORIGINALLY come from' ..no matter what..

i.e. the architecture should be such that when you press the BACK BUTTON on Page 1 it should always take me to the ORIGINAL previous page ... in my case 'Main Menu Page' .. So I don't have to use all this 'ShowPageandRemovePreviousPages' or 'ShowPage' etc... Because I personally feel the 'BACK BUTTON on PAGE 1' is meant to take you to the 'ORIGINAL previous Page' i.e. the 'Main Menu page' (in my case) ... (and should have no dependency on from WHERE I have given control back to this 'Page 1' - whether it was given from 'Page 2' or whether it was given from some other page viz. Page X etc. for that matter.. The previous page for 'Page 1' should always be the Original Page viz. the 'Main Menu Page'..)..

I personally am unable to appreciate this 'ShowPage' feature in which pressing the 'back button on Page 1' takes you back to the 'calling page' viz. 'Page 2' (if it was called from 'Page 2')...'ShowPageandRemovePreviousPages' sounds logical (in the sense that the 'Track to Page 2' is removed but then it should not have any bearing on the 'BACK BUTTON' of Page 1 .. it should always take me back to the ORIGINAL Page.. viz. 'Main Menu page' in my case)...

.... Maybe I lack experience...


Now, regarding the 'Home Button' solution that you suggested, suppose I want to put a 'Home button' in the 'Top Bar where the usual B4xPage back button is shown'. then how does one put a button on the Top Bar of a b4xPage (note that I have no intention to take the default BACK BUTTON position.. the Home button can be on the right end)..

Thanks
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I find this weird ...

I personally feel that the end-user should be able to 'Go back the way they have ORIGINALLY come from' ..no matter what..
I don't understand that, as that's right, you can go back to the calling page and you call Page 1 from Page2, not from MainPage.

What you would like is that "back" should follow your "numerical/logical order", in a sense.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
As @LucaMs said if you remove a page from the stack then the page is not available through navigation arrows.

But I think the following will work in a natural way if you don't remove pages, just .ShowPage

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private Button1 As Button
    Private AllowClosing As Boolean
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")   'This is page2 and this is just to get a button
End Sub

Private Sub B4XPage_Appear
    Log("Page 2 APPEAR")
    AllowClosing = False
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    If AllowClosing Then Return True Else Return False
End Sub

Private Sub Button1_Click
    AllowClosing = True
    B4XPages.ClosePage(Me)
    'B4XPages.ShowPage("page1")   'You don't need to do this
End Sub
 

Attachments

  • pgsTest2.zip
    15.3 KB · Views: 95
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I revised the zip file in #7. One line was not needed.
B.T.W. You gave an unusually clear explanation of what you wanted to do.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I need to study a bit (maybe even the source of B4XPages.b4xlib) but I have a trick in mind that might work ...

WARNING: tested very little.

Public Sub to be added to a code module. It should allow you to set (CHANGE) the page to be shown when pressing the "back button".

In a code module:
'PageID must exist, of course.
Public Sub SetPrevPageID(PageID As String)
    ' Just to shorten the source code.
    Dim lstPageIDs As List = B4XPages.GetManager.mStackOfPageIds.AsList
 
    Dim PageIDIndex As Int = lstPageIDs.IndexOf(PageID)
    If PageIDIndex >= 0 Then
        lstPageIDs.RemoveAt(PageIDIndex)
    End If
 
    Dim TopPageID As String = B4XPages.GetManager.GetTopPage.Id
    Dim TopPageIndex As Int = lstPageIDs.IndexOf(TopPageID)
 
    If TopPageIndex > 0 Then
        lstPageIDs.InsertAt(TopPageIndex - 1, PageID)
    Else
        lstPageIDs.Add(PageID)
    End If
 
'    Log("new stack:")
'    For Each ID As String In lstPageIDs
'        Log(ID)
'    Next
End Sub

Usage:
    B4XPages.ShowPage("PageOne")
    modProjUtils.SetPrevPageID("mainpage")
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
You can finesse the flow by using in page1 B4XPages.ShowAndRemovePreviousPages("page2") to remove the back arrow on page 2
And then rebuild the stack on page2 button click

B4X:
Private Sub B4XPage_Appear
    Log("Page 2 APPEAR")
    AllowClosing = False
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    If AllowClosing Then Return True Else Return False
End Sub

Private Sub Button1_Click
    AllowClosing = True
    B4XPages.ShowPage("MainPage")
    B4XPages.ShowPage("page1")
End Sub
 

Attachments

  • pgsTest3.zip
    15.4 KB · Views: 83
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You can finesse the flow by using .ShowAndRemovePreviousPage("page2") to remove the back arrow on page 2
And then rebuild the stack on page2 button click

B4X:
Private Sub B4XPage_Appear
    Log("Page 2 APPEAR")
    AllowClosing = False
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    If AllowClosing Then Return True Else Return False
End Sub

Private Sub Button1_Click
    AllowClosing = True
    B4XPages.ShowPage("MainPage")
    B4XPages.ShowPage("page1")
End Sub
That way, MainPage would be shown first, of which the event-routine B4XPage_Appear will also be executed, so it's best not to.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@LucaMs

That's true (although I don't recall having a need for B4XPage_Appear in B4XMainPage).
In my test the show part is fast enough not to see it.
Your method of building the stack yourself is very interesting.
 
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
WARNING: tested very little.

Public Sub to be added to a code module. It should allow you to set (CHANGE) the page to be shown when pressing the "back button".

In a code module:
'PageID must exist, of course.
Public Sub SetPrevPageID(PageID As String)
    ' Just to shorten the source code.
    Dim lstPageIDs As List = B4XPages.GetManager.mStackOfPageIds.AsList
 
    Dim PageIDIndex As Int = lstPageIDs.IndexOf(PageID)
    If PageIDIndex >= 0 Then
        lstPageIDs.RemoveAt(PageIDIndex)
    End If
 
    Dim TopPageID As String = B4XPages.GetManager.GetTopPage.Id
    Dim TopPageIndex As Int = lstPageIDs.IndexOf(TopPageID)
 
    If TopPageIndex > 0 Then
        lstPageIDs.InsertAt(TopPageIndex - 1, PageID)
    Else
        lstPageIDs.Add(PageID)
    End If
 
'    Log("new stack:")
'    For Each ID As String In lstPageIDs
'        Log(ID)
'    Next
End Sub

Usage:
    B4XPages.ShowPage("PageOne")
    modProjUtils.SetPrevPageID("mainpage")
Hi Lucas,

Wow ! Looks like an 'Out of the Box' solution..

Will try it out...

Thank you...
 
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
As @LucaMs said if you remove a page from the stack then the page is not available through navigation arrows.

But I think the following will work in a natural way if you don't remove pages, just .ShowPage

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private Button1 As Button
    Private AllowClosing As Boolean
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")   'This is page2 and this is just to get a button
End Sub

Private Sub B4XPage_Appear
    Log("Page 2 APPEAR")
    AllowClosing = False
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    If AllowClosing Then Return True Else Return False
End Sub

Private Sub Button1_Click
    AllowClosing = True
    B4XPages.ClosePage(Me)
    'B4XPages.ShowPage("page1")   'You don't need to do this
End Sub
Hi William !

Thanks for helping..

I think you will agree with me that Lucas solution does seem to be very good..(but as he has Warned.. it needs to be tested more)..

I will try both yours and Lucas's...

Thank you very much....
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Based on the creative technique of @LucaMs it is possible to rebuilt the stack without showing the previous pages.
This works. Don't you love B4X?

B4X:
Private Sub B4XPage_Appear
    Log("Page 2 APPEAR")
    AllowClosing = False
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
    If AllowClosing Then Return True Else Return False
End Sub

Private Sub Button1_Click
    AllowClosing = True
    B4XPages.GetManager.mStackOfPageIds.clear
    B4XPages.GetManager.mStackOfPageIds.Add("mainpage")
    B4XPages.GetManager.mStackOfPageIds.Add("page1")
    B4XPages.GetManager.mStackOfPageIds.Add("page2")
    B4XPages.ClosePage(Me)
End Sub
 
Upvote 0
Top