Android Question B4XPages- Why Main Page is Shown Ahead of Page2

Mahares

Expert
Licensed User
Longtime User
Very simple example got me more confused. I thought I started getting a little handle on this. Why isn't Page2 displayed after B4XMainPage is loaded.:
B4X:
'Code in B4XMainPage
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private P2 As Page2
End Sub

Public Sub Initialize
    P2.Initialize
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")  'has one button
    B4XPages.AddPage("Page2",P2)
    B4XPages.ShowPage("Page2")
End Sub

B4X:
'Page2 code:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Public Label1 As B4XView
End Sub

Public Sub Initialize As Object
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("2")   'has a label only
End Sub
To explain it better: I want Page2 to be the one visible when the app runs, not B4XMainPage.
 
Last edited:

josejad

Expert
Licensed User
Longtime User
Welcome to my world


I'm "adapting" the B4XDrawer example to do this, but I'm getting all kind of errors. Stay tuned¡¡ :-D
 
Upvote 0

Sia Kong Lam

Member
Licensed User
Longtime User
Work around. Might not be the best solution. Added Sleep(0) between B4xPages.AddPage and Take out B4XPages.ShowPage

B4XPages.AddPage("Page2",P2)
Sleep(0)
B4XPages.ShowPageAndRemovePreviousPages("Page2")
' B4XPages.ShowPage("Page2")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Might not be the best solution
Your code shows Page2, but there is no way to go back to mainpage.

I'm getting all kind of errors.
I have followed your thread . But mine is much simpler and still could not figure it out. I have been using B4XPages for a while, but in the past I always get the main page to be the starting point. This is a different case where I want Page2 to be shown, not the main page and still be able to go bsck to mainpage.
 
Upvote 0

Sia Kong Lam

Member
Licensed User
Longtime User

Page 2 add this routine

Private Sub B4XPage_CloseRequest As ResumableSub
B4XPages.ShowPage("MainPage")
Return True
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User

Attachments

  • Project.zip
    15.3 KB · Views: 164
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Maybe something like this?
Your code works Jose. The one thing that baffles me is this line: B4XPages.AddPage("Main", Me) in the below sub of B4XMainPage:
B4X:
Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
    Dim Page_2 As Page2
    Page_2.Initialize
    B4XPages.AddPage("Main", Me)  'why is this line needed here?
    B4XPages.AddPage("Page 2", Page_2)
    B4XPages.ShowPage("Page 2")
End Sub
Isn't B4XMainPage added and created in the Sub: Private Sub B4XPage_Created (Root1 As B4XView)
I thought it would be simpler than this.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Isn't B4XMainPage added and created in the Sub: Private Sub B4XPage_Created
I'm still a little confused about that, but I've tried and if I don't add the line B4XPages.AddPage("Main", Me), it seems B4XMainPage is not added to the stack
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
B4X:
'You need this in the "page2" class or a sleep(0) after loading layout to allow layout loading to complete
'Normally it works because of the MainPage shows first

Private Sub B4XPage_Appear
    B4XPages.ClosePage(B4XPages.MainPage)
End Sub

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")   ' if you don't need this remove it

    Dim p2 As page2
    p2.Initialize
    B4XPages.AddPage("Page 2", p2)
    B4XPages.ShowPage("Page 2")      
End Sub
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You need this in the "page2" class
Your code brings up Page2, but does not allow me to go back to B4XMainPage unless I add a button or label click event in Page2 like this:
B4X:
Sub Label1_Click
    B4XPages.ShowPage("MainPage")
End Sub
Is that how it is supposed to work.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Using a stripped down version of Three Pages Example , the sample below does what your trying to achieve.
There is only 2 Events used .. Button_Click to switch between pages .. (No Page_Appear, Initializes etc...)

The main goal was to keep this a simple as possible (minimal code)
As suggested above ... just adding Sleep(0) to your code above solved the issue.

B4X:
'Main Page ...
Private Sub B4XPage_Created (Root1 As B4XView)
  Root = Root1
  Root.LoadLayout("pageMain")
  Sleep(0)
  
  Page2.Initialize
  B4XPages.AddPage("Page2", Page2)
  B4XPages.ShowPage("Page2")
End Sub
 

Attachments

  • B4APages Example.zip
    28.5 KB · Views: 171
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@mangojack is right that his is the simplest solution. However, you can call B4XPages.ShowPage("MainPage") anywhere. It doesn't need to be part of a button.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Your code works Jose
It works, but it seems this way the B4XPage_Appear event is not fired first time.

Just add to the Page2:
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private Label1 As B4XView
End Sub

Sub B4XPage_Appear
   Label1.Text = "Page Appear"
End Sub

And you will see when you run the app, the Label1.Text remains "This is Page2". Press home an go back to the app, and it will change.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It is fixed in v1.08.
"B4XPages v1.08 - fixes an issue where the B4XPage_Appear event is not raised when the first page that is shown is not the main page."

I have not read the thread, sorry, but, as in the past the activity Main was immediately shown, now the B4XMainPage is shown.
I see no reason to skip this one and open a different one first.

However, if it suits you, I'm happy
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
I went crazy with that topic in the first project with B4XPages.
After a lot of reading, searching, questioning, and racking my mind, I decided there was a problem and it wasn't working right. I left it waiting for better times.
It seems that with that new version it is solved, I'm glad, I'll go back to my project.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…