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: 9

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

carlbertmx

New Member
Hi thanks for your reply

I understand the B4XPages philosophy. act_Uno is an Activity for a 'heavy' volatile registration process with resources that will never be used again after the first run.

I chose this approach to guarantee:


  • Memory Footprint: Total destruction of UI objects and logic via Activity.Finish once finished.
  • Lean Start: B4XPages only initializes after the app registration process is complete.
Is there another way in B4A to achieve this level of 'total resource destruction' without using an external activity?
 
Upvote 0

carlbertmx

New Member
Hi josejad
Thanks for the code. However, my main goal is a clean start.

If I put the login/registration logic inside B4XMainPage (even with RemoveAllViews), the B4XMainPage instance will always carry the memory overhead (variables, objects, and logic) of a process that only happens once.

Is there a way in B4X to start B4XMainPage completely clean, without any memory footprint from the app/user registration process? In my current 'Gatekeeper' approach, I use a separate Activity for registration and then Activity.Finish. This way, when B4XMainPage finally initializes, it contains only the code and resources directly related to the app's core operation, with zero 'leftover' RAM from the registration phase." thans for your reply
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
with zero 'leftover' RAM from the registration phase."
Hi:

I will left Erel answer this kind of questions but... have you ever had any RAM problem due to some variables in B4A?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I understand the B4XPages philosophy. act_Uno is an Activity for a 'heavy' volatile registration process with resources that will never be used again after the first run.

I chose this approach to guarantee:


  • Memory Footprint: Total destruction of UI objects and logic via Activity.Finish once finished.
  • Lean Start: B4XPages only initializes after the app registration process is complete.
Is there another way in B4A to achieve this level of 'total resource destruction' without using an external activity?
There is no relation between the "heavy resources" and the activity. On the contrary, the less you implement in an activity the better as the activity has a very complex life cycle and you will need to handle situations where the activity is paused while the registration process is running.

As I wrote, adding an activity only makes things more complicated (more accurately: it will also hurt performance and reliability).
 
Upvote 0
Top