Lifecycle & Architecture

GuyBooth

Active Member
Licensed User
Longtime User
Well I've read the tutorials and I thought I understood how Android worked - but apparently not!

On startup I want to check for the existence of a database and if it does not exist, suspend the Main Activity and enter a configuration activity so the user can set up file locations.

On startup, if the database exists, I then want to check for the existence of a database update file, and if it exists, pause the Main Activity and enter an Update Activity.

In both of the above cases, I wanted to suspend the "First Time" code in the Main Activity while the other activities were opened, completed and closed; continuing the Main Activity before these "diversions" will otherwise produce errors and essentially crash the application.

What I am finding is that by the time the Setup or Update Activities are started, the rest of the Main startup activity has still been attempted - which was not my intention. I don't want to put everything in one big Main Activity - that doesn't feel right.

Can anyone help me out here? What is a better way to approach this?
 

thedesolatesoul

Expert
Licensed User
Longtime User
You cannot suspend an activity. (whatever that means), it only gets paused.
You have two subs Activity_Create and Activity_Resume. Whenever you will come out of the update or config activity, you will return to your main activity and the Activity_Resume sub will execute. You just need to set a process global flag which tells your which activity you went/returned from.

Also, FirstTime does not mean the first time your app was run. It means the first time the process is created. This is where you initialize all your process globals.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Or put the code that is currently in your Main Activity into another new Activity.

The Main Activity when started decides whether to start this new Activity or to start the Configuration or Update Activity.

The Configuration and Update Activities can then start this new Activity when they have completed.

Keeps the flow of Activities logical.

Martin.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
I'm now working on this approach.

Or put the code that is currently in your Main Activity into another new Activity.

The Main Activity when started decides whether to start this new Activity or to start the Configuration or Update Activity.

The Configuration and Update Activities can then start this new Activity when they have completed.
.

I still struggle to understand what can be done inside the "First Time" loop and what must be done in Create_Activity but Outside the "First Time" loop. What triggers a "First Time" v just a Create Activity?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Android apps consist of:
- a process
- one or more activities

When the app is initially invoked the process is created, hence the FirstTime flag is true. If the app is already in memory, but just brought from the background to the foreground FirstTime is not true, and anything in Process_Globals retains state.

Activity_Create is fired whenever an activity is 'created'. They may be cycled out due to resource freeing.
Activity_Resume always fires whenever the activity comes to the foreground.

FirstTime = Initialize Process Globals
Create = Create your layouts, initialize/loads variables for Globals
Resume = Update activity state
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
I restructured everything and would be happy with the flow and organization - but it still isn't working the way I expect. Here is the code I have in the Sub Resume section of the activity. (I haven't included the top part):
B4X:
If TMM.gbDBExists Then
    ' Open the database. Don't create it if it does not exist
    If TMM.gsqlMB.IsInitialized = False Then
        TMM.gsqlMB.Initialize(TMM.gsDBPath,TMM.gsDBName,False)
    End If   
Else
    If Msgbox2("Sorry - could not find ..." & CRLF & _
        TMM.gsDBPath & "/" & TMM.gsDBName & _
        "Do you want to change the configuration settings," & CRLF & _
        "or Exit Application?","Main Database", _
        "Configure",TMM.NullString,"Exit",TMM.gbmpLogo36x36) = _ 
         DialogResponse.POSITIVE Then
       ' Return to the settings file
        StartActivity(TMM_Settings)
    Else
        Activity.Finish
    End If
End If
StartActivity(TMM_Run)
End Sub
Despite clicking "Exit" when asked, and I have proved that when I do this the program does go to the "Activity.Finish" statement, nevertheless the programm continues to Start the TMM_Run Activity.
I don't understand why - and I don't know how to stop it!
 
Last edited:
Upvote 0
Top