Android Question Best practice for conditional B4XPages initialization (Login/Registration flow)

carlbertmx

New Member

"Hi Erel and community,​

I'm implementing a registration flow in a B4A B4XPages project. I want to ensure that the B4XPages environment only initializes after a successful registration/login process handled by an external Activity (act_Registration).

My current approach in Main Activity:
  1. In Activity_Resume, I check a global flag AppRegistered.
  2. If False, I call StartActivity(act_Registration) and Return.
  3. If True, I initialize B4XPagesManager (if not initialized) and delegate the rest of the life cycle.
In act_Registration:
  1. I handle the UI and logic.
  2. Upon success, I set Main.AppRegistered = True and call Activity.Finish.
  3. I also use Return True in Activity_KeyPress (KeyCodes.KEYCODE_BACK) to force the user to stay in the registration flow.
My questions:
  1. Is this the recommended way to "gatekeep" B4XPages initialization?
  2. Are there any side effects regarding the BackStack or the B4XPages life cycle by delaying B4XPagesManager.Initialize this way?
  3. In Main, I'm using moveTaskToBack(true) via JavaObject on the Back key to keep the app alive instead of closing it. Is this compatible with B4XPages internal navigation?
Thanks in advance for your guidance."
 

Attachments

  • Insu.zip
    11.4 KB · Views: 2

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi:

Maybe the easiest wait would be:
B4X:
    'B4XMain_Page->B4XPage_Created
    If UserIsLogged Then 'UserIsLogged could be a variable saved in KVS when the user logs in for first time
        ShowDashboard
    Else
        Login
    End If

Private Sub Login
    Root.LoadLayout("Login") 'Login
    B4XPages.SetTitle(Me, "Login")
End Sub

Public Sub ShowDashboard
    Root.RemoveAllViews 'You can call ShowDashboard not only when you open the app starts and the user is logged, but the first time the user logs in, so you want to hide the Login layout
    B4XPages.SetTitle(Me, "Dashboard")
    Root.LoadLayout("Dashboard")
    '... rest of your code
End Sub


You can take a look to these posts, too, as you can see Erel says in the #11 post: "Note that the first page added, in the Initialize sub, will be shown automatically."

 
Last edited:
Upvote 0
Top