Android Tutorial Android Process and activities life cycle

Erel

Administrator
Staff member
Licensed User
There is no accepted way to close your app. Only the user can close it.

You can kill it with NativeObject however your app will be rejected from the App Store.
 

Alberto Michelis

Active Member
Licensed User
I have a problem with Process_Globasl variables.
May be I dont understand well

I have a Main Activity in which I manage the billing addin, the upgrade fo the db, and all sort of init process of the app.
This Main activity is never called again.
It has many Process_Global variables that are used for many other activitys and by a Service.
This is the first time I realized it but once the app was in the background a lot of time, and I restore, it these variables are blanked.

What am I doing wrong?
 

ilan

Expert
Licensed User
I have a problem with Process_Globasl variables.
May be I dont understand well

I have a Main Activity in which I manage the billing addin, the upgrade fo the db, and all sort of init process of the app.
This Main activity is never called again.
It has many Process_Global variables that are used for many other activitys and by a Service.
This is the first time I realized it but once the app was in the background a lot of time, and I restore, it these variables are blanked.

What am I doing wrong?
After a while an activity goes to the background android will kill it so its importent to store your variables

so if you send an activity to the background and dont use it again and you exit the second activity and return to it (returning to your app will open the last activity so activity 2) the variables in activity 1 may be blank thatswhy you should after starting your 2nd activity from activity 1
get all variables you need and store them in activity 2

if you use a service you better save your data (Process_global variables) to a text file and read from it in your service module...
 

Alberto Michelis

Active Member
Licensed User
But in the first post of this thread, Erel said that Process_Globals are process variables and are alive till the process is killed and, this time, the process is not killed, only the activity is in the backgound.
Besides it works ok during the day, but it olny happened when it is a long time in background.
 

Alberto Michelis

Active Member
Licensed User
Then you mean there are not Process Global variables that are accessed by all activities?
This is not true, Im using these Process variables and work ok.
I only have this problem when the app is a long time in backgroud.
 

ilan

Expert
Licensed User
Then you mean there are not Process Global variables that are accessed by all activities?
Where do you see in my answers what you wrote??

I just try to help and if you would click on the link i have posted above you would not keep asking...
 
Last edited:

Alberto Michelis

Active Member
Licensed User
I did, but I dont understand what you think I will find in that link, english is not my mother tong so I´m slow to undestand.
 

corwin42

Expert
Licensed User
I did, but I dont understand what you think I will find in that link, english is not my mother tong so I´m slow to undestand.
Your problem is that you have to understand that your app can start with every Activity, not only with the first one.

When your app is sent to the background it is possible that the system decides to kill the process completely. If you return to your app, it will be started with the activity which was last active. Because the initialization of the process global variables is only in your first Activity they will be emty.

You will need to do global initializations in all of your Activities.
 

LucaMs

Expert
Licensed User
Recently I read this Erel's answer.

I must say that I do not understand one thing: if the operating system kills an app, the app should always restart with the activity set as LAUNCHER in the Manifest file.
 

aeric

Active Member
Licensed User
I have a problem with Process_Globasl variables.
May be I dont understand well

I have a Main Activity in which I manage the billing addin, the upgrade fo the db, and all sort of init process of the app.
This Main activity is never called again.
It has many Process_Global variables that are used for many other activitys and by a Service.
This is the first time I realized it but once the app was in the background a lot of time, and I restore, it these variables are blanked.

What am I doing wrong?
My guest of your issue is how you load the layout in Main activity and calling the variables you have declared in Main activity in your Second activity.
Here is how I normally do:
First, declare some global variables in Main activity Sub Process_Globals
B4X:
Sub Process_Globals
    Dim DBFileDir As String
    Dim DBFileName As String
    Dim DB1 As SQL
End Sub
In Main activity, initialize the variables
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmMain")
    If FirstTime Then
        DBFileDir = File.DirDefaultExternal
        DBFileName = "mydata.db"
        If File.Exists(DBFileDir, DBFileName) = False Then
            DBFileDir = DBUtils.CopyDBFromAssets(DBFileName)
        End If
        DB1.Initialize(DBFileDir, DBFileName, False)
    End If
End Sub
When you want to call your variables in Second activity, you need to write the activity module, "dot" then your variable.
B4X:
Sub Process_Globals
    Dim DB2 As SQL
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmSecondActivity")
    If FirstTime Then       
        DB2.Initialize(Main.DBFileDir, Main.DBFileName, False)
    End If
End Sub
I always put the Activity.LoadLayout outside or before the "If FirstTime Then"
 

aeric

Active Member
Licensed User
One more thing is, DO NOT use "Activity.Finish" to unload the Activity when you want to Start another Activity.
B4X:
Activity.Finish
 
Top