Android Question Problem navigating to another page in the B4XPage_Created event

Hello,
I am having a problem navigating to the SignInPage in the B4XMainPage and B4XPage_Created event.
The code I am using is as follows:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    root=Root1
    
    If Login=False Then
        Dim SignInPage As SignInPage
        SignInPage.Initialize
        B4XPages.AddPage("SignInPage",SignInPage)
        B4XPages.ShowPageAndRemovePreviousPages("SignInPage")
    End If
End Sub
This code is supposed to check if the user is logged in or not. If the user is not logged in, they should be redirected to the SignInPage. However, when I run this code, it does not redirect to the SignInPage and remains on the B4XMainPage.

Please provide guidance on how to resolve this issue.
 

LucaMs

Expert
Licensed User
Longtime User
Hello,
I am having a problem navigating to the SignInPage in the B4XMainPage and B4XPage_Created event.
The code I am using is as follows:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    root=Root1
  
    If Login=False Then
        Dim SignInPage As SignInPage
        SignInPage.Initialize
        B4XPages.AddPage("SignInPage",SignInPage)
        B4XPages.ShowPageAndRemovePreviousPages("SignInPage")
    End If
End Sub
This code is supposed to check if the user is logged in or not. If the user is not logged in, they should be redirected to the SignInPage. However, when I run this code, it does not redirect to the SignInPage and remains on the B4XMainPage.

Please provide guidance on how to resolve this issue.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Public Login As Boolean
    Public SignInPage As SignInPage
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
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")
   
    SignInPage.Initialize
    B4XPages.AddPage("SignInPage", SignInPage)
   
    If Login = False Then
        CallSubDelayed(Me, "OpenSingInPage")
    End If
End Sub

Private Sub OpenSingInPage
    B4XPages.ShowPageAndRemovePreviousPages("SignInPage")
End Sub
 
Last edited:
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
You can make the B4XMainPage invisible, but I don't think it really disappears, because that's the class where all the variables for all other B4XPages and the B4XPageManager are.

If you look at Erel's ThreePagesExample, the login layout is first loaded in B4XMainPage. You must first add the validate of the login conditions in the Sub btnLogin_Click. If this is not met, you will not open the next B4XPage.
This prevents the next page from already being tested for a successful login validation when it is created, which, in all probability, has not even been performed yet, and perhaps never tested after.

If you follow this principle, the next B4XPages will only be displayed after a successful login. If someone could log out on an other B4XPage(s), then you should not put a successful login validation in the Sub B4XPage_Created, but in the Sub B4XPage_Appear, while making sure that there is always only one B4XPage showed at a time, otherwise the validation test will not be performed. Based on how you do it, possibly you need to enforce a reload of that B4XPage to force a retest of the login status.
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
your problem is with B4XPage_Created , it is only executed once.


Check the example that Erel does in this link:

 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
login.gif







B4XMainPage
B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public kvs As KeyValueStore
    
    Public dashboard As pDashboard
    
    
    Private edtName As B4XView
    Private btnLogin As B4XView
    Private imgLogin As B4XImageView
End Sub

Public Sub Initialize
    
'    B4XPages.GetManager.LogEvents = True
    kvs.Initialize(xui.DefaultFolder, "ksv.db")
    
    'se já estiver logado abre a proxima tela
    If kvs.ContainsKey("user") Then
        Log("logged in user:" & kvs.ContainsKey("user"))
        B4XPages.AddPage("dashboard", dashboard.Initialize)
        B4XPages.ShowPageAndRemovePreviousPages("dashboard")
    Else
        Log("no user logged in")
    End If
End Sub


Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("login")
    
    If dashboard.IsInitialized = False Then 
        B4XPages.AddPage("dashboard", dashboard.Initialize)
    End If
    
    imgLogin.Bitmap = xui.LoadBitmap(File.DirAssets, "logo.png")
    edtName.SetColorAndBorder(0xFFF2F2F2,1dip, 0xFFD8D8D8, 10dip)
    btnLogin.SetColorAndBorder(0xFF02BDDE,0, 0, 10dip)
End Sub

Private Sub B4XPage_Appear
End Sub


Private Sub btnLogin_Click
    kvs.put("user", edtName.Text)
    B4XPages.ShowPageAndRemovePreviousPages("dashboard")
End Sub


pDashboard
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private cpbCard As CircularProgressBar
    Private lblProgressCard As B4XView
    Private lblName As B4XView
End Sub

Public Sub Initialize As Object
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("dashboard")
End Sub

Private Sub B4XPage_Appear
    lblName.Text = "Hello, " & B4XPages.MainPage.kvs.Get("user") & "!"
    cpbCard.Value = Rnd(25,75)
    lblProgressCard.Text = cpbCard.Value & "%"
End Sub

Private Sub pnlFooter1_Click
    B4XPage_Appear
End Sub

Private Sub pnlFooter2_Click
    B4XPage_Appear
End Sub

Private Sub pnlFooter3_Click
    B4XPage_Appear
End Sub

Private Sub pnlFooter4_Click
    B4XPage_Appear
End Sub

Private Sub pnlFooter5_Click
    B4XPages.MainPage.kvs.Remove("user")
    B4XPages.MainPage.kvs.Vacuum
    B4XPages.ShowPageAndRemovePreviousPages("MainPage")
End Sub
 

Attachments

  • projectLogin.zip
    33.6 KB · Views: 17
Upvote 0
Top