Android Question [B4XPages] How to quickly load a B4XPage without showing MainPage

José J. Aguilar

Expert
Licensed User
Hi:

I'm starting to play with B4XPages, just adapting this example
It has a login page an then 2 others.
What I want to do is to save the logged user (i.e. in KVS) and, from then on, once you start the app check if the user is already logged, then go to the next page instead of B4XMainPage.

I've tried three different ways, each one with its drawbacks.

First of all, I save the login in KVS

B4X:
Sub btnLogin_Click
    KVS.Put("user", txtUser.Text.Trim)
    B4XPages.ShowPageAndRemovePreviousPages("Page 2")
End Sub


First way
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    B4XPages.GetManager.LogEvents = True
    Root = Root1
    KVS.Initialize(File.DirInternal,"kvs")
    Page2.Initialize
    B4XPages.AddPage("Page 2", Page2)
    Page3.Initialize
    B4XPages.AddPage("Page 3", Page3)
    Root.LoadLayout("Login")
End Sub

Sub B4XPage_Appear
    If KVS.ContainsKey("user") Then
        B4XPages.ShowPageAndRemovePreviousPages("Page 2")
    End If
End Sub
This way (I think this is the right one), when you open the app, you see (even in Release Mode) how everything happens, in the Title bar: "BXAExample -> MainPage-> Page 2"
The effect here is minor because of the low frame rate.

Test1.gif



Second way
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    B4XPages.GetManager.LogEvents = True
    Root = Root1
    KVS.Initialize(File.DirInternal,"kvs")
    Page2.Initialize
    B4XPages.AddPage("Page 2", Page2)
    Page3.Initialize
    B4XPages.AddPage("Page 3", Page3)
    If KVS.ContainsKey("user") Then
        B4XPages.ShowPageAndRemovePreviousPages("Page 2")
    Else
        Root.LoadLayout("Login")
    End If
End Sub
This way, when you've saved the user in KVS, if you start the app again, it get stucked in MainPage until you touch in some place:

Test2.gif


Third way:
B4X:
Sub B4XPage_Appear
    If KVS.ContainsKey("user") Then
        B4XPages.ShowPageAndRemovePreviousPages("Page 2")
   else
        Root.LoadLayout("Login")
    End If
End Sub
Problem: Obviously, if close the app and open it again, the layout is reloaded over the first one when B4XPage_Appear
And, as the B4XTutorial says:
4. B4XPage_Created This is the place to load the layout.

And:
Tip: don't treat B4XPages as activities. In most cases you don't need to do anything in the Appear and Disappear events.

Attached the project with the second option.
 

Attachments

  • Project.zip
    197.3 KB · Views: 81

José J. Aguilar

Expert
Licensed User
Did you try this?
No, I miss that one. It runs better, but with the code in B4XPage_Appear, what Erel doesn't recommend. Thanks

Check the state in the Initialize sub of B4XMainPage.
If you show a page here then B4XMainPage will not be shown automatically.
Hi Erel,
I can initializce KVS there, but I can't show a page from Initialize due to B4XPage_Created is not yet fired so the rest of B4XPages are not created yet.
Not sure if I've understood right.

B4X:
Public Sub Initialize
    KVS.Initialize(File.DirInternal,"kvs")
    B4XPages.GetManager.TransitionAnimationDuration = 0

     'I can't run this code here because "Page 2" still doesn't exist
    If KVS.ContainsKey("user") Then
        Log (KVS.Get("user"))
        B4XPages.ShowPageAndRemovePreviousPages("Page 2")
    End If
End Sub

Thanks to both of you
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
B4X:
Public Sub Initialize
    KVS.Initialize(File.DirInternal,"kvs")
    B4XPages.GetManager.TransitionAnimationDuration = 0
    Dim p2 As Page2
    p2.Initialize("")
    B4XPages.AddPage("Page 2", p2)
     'I can't run this code here because "Page 2" still doesn't exist
    If KVS.ContainsKey("user") Then
        Log (KVS.Get("user"))
        B4XPages.ShowPage("Page 2")
    End If
End Sub
 
Upvote 0

José J. Aguilar

Expert
Licensed User
Hi Erel:

You're right, if I move this code from B4XPage_Appear to B4XPage_Create, the Hamburger is created first time.
But this code is "as it" from your [B4X] B4XPages + B4XDrawer example so I though it should be there.


B4X:
Private Sub B4XPage_Appear
    #if B4A
    Sleep(0)
    B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
    Dim bd As BitmapDrawable
    bd.Initialize(HamburgerIcon)
    B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))
    #End If
End Sub
 
Upvote 0

José J. Aguilar

Expert
Licensed User
Here you got it.
I have had some troubles to adapt it, but here it is.
It's the B4XPages + B4XDrawer adapted to show Page 2 if the user has previously logged in.

If you call B4XPages.ShowPageAndRemovePreviousPages("Page 2") from btnLogin, it works and shows the hamburger.
B4X:
Sub btnLogin_Click
    KVS.Put("user", txtUser.Text.Trim) 'ADDED LINE TO SAVE THE LOGGED USER
    B4XPages.ShowPageAndRemovePreviousPages("Page 2")
End Sub

But if you close the app, and then reload and you call from Initialize, it's not shown, just after hide the app and reload again, then B4XPage_Appear is fired and the hamburger appears:
B4X:
Public Sub Initialize

    'ADDED CODE, CHECK IS USER IS LOGGED THEN GO TO PAGE 2 DIRECTLY
    B4XPages.GetManager.LogEvents = True
    KVS.Initialize(File.DirInternal, "kvs")
   
    If KVS.ContainsKey("user") Then
        Page2.Initialize
        B4XPages.AddPage("Page 2", Page2)
        Page3.Initialize
        B4XPages.AddPage("Page 3", Page3)
        B4XPages.ShowPageAndRemovePreviousPages("Page 2")
    End If
End Sub
 

Attachments

  • Project.zip
    197.4 KB · Views: 107
Upvote 0
Top