Android Tutorial Android Process and activities life cycle

Hennell

Member
Licensed User
Longtime User
I'm finally making the switch to Android, so have been looking over the tutorials and trying to understand how these default subs are called and when; ended up making a flow chart which I think indicates what happens, Erel is this correct or am I still missing something?

Basic4Android process flow chart.
 

mjcoon

Well-Known Member
Licensed User

Is this neat behaviour wrecked when running under the IDE in debug mode? I ask because I am seeing multiple strings overlaid in a Label in my simple program when I induce a pause/resume. This looks as if multiple instances of the label object are being created and mapped onto the same view. (Not sure if I have the terminology right; I've only just started having another go at B4A!) Edit: No, it was not that; I had wrongly thought I needed to do LoadLayout() at each resume (because the Activity would have been destroyed!)...

Mike.
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
Finish & Start

It's possible to programmatically finish the current activity and then start another activity ?

Activity.Finish
StartActivity("OtherActivity")
 

luke2012

Well-Known Member
Licensed User
Longtime User
I'm glad to known that!

I was in doubt about this because I believe that the Activity.Finish close the activity and the next statment will not execute.

Thanks for the info Erel!
 

luke2012

Well-Known Member
Licensed User
Longtime User
I understood!
It is also possible to call Activity.Finish within the Activity_Pause event to destroy the activity when I leave it (for example when I start a new activity)?
 

sortec.nick

Member
Licensed User
Longtime User
I understood!
It is also possible to call Activity.Finish within the Activity_Pause event to destroy the activity when I leave it (for example when I start a new activity)?

Yes, but it is also important to bear in mind that Activity_Pause is called in every case that the current Activity loses focus i.e. when the phone display goes to sleep or the user accesses the Settings menu or any other application.

One solution might be to pass a focus token between activities that knows which activities to leave open. You might wish to later make a Breadcrumbs object that tracks movement between activities.
 

luke2012

Well-Known Member
Licensed User
Longtime User

What do you mean when you say focus token ?
 

Turbo3

Active Member
Licensed User
Longtime User
Some of my users report losing their setup values. My app reads in a previously saved setup file in Activity_Create when FirstTime=true and the file exists. So at first I did not see how this could be happening. When their setup file is not read in, default settings are used which on exit then overwrite their saved settings.

However, I now see a hole that could explain this happening. The only routine who's code is guaranteed to execute all the way through is Activity_Pause. Code in Activity_Create could get skipped if an orientation change happens while code in Activity_Create is being execute. So there is a race when Activity_Create starts executing and android needing to destroy it because of an orientation change. The majority of the time Activity_Create will finish first. But if not then the next time it starts the FirstTime flag will be false and any code conditioned to execute by FirstTime will not get executed and since it did not get executed (or execute to completion) the first time because of the orientation change it never gets execute.

The solution to this is not to rely on FirstTime to mean that all the code in Activity_Create has been executed but use your own flag set when all the things that need to be done once have completed. If this new flag is not set then the next time Activity_Create is executed do it again until the flag is finally set. The difference is that FirstTime is set when Activity_Create is first called (i.e. at the top of the code) and this new flag you create would be set at the end of Activity_Create signaling all the first time code you need got executed.

I think most people would not need this extra level if their code in Activity_Create is short and fast. But if you are reading in a large setup file you are exposed to being yanked out of it by Android before it completes while at the same time having FirstTime set to true.

Anyway, that is my best guess at what must be happening when a user loses their setup file.
 

Turbo3

Active Member
Licensed User
Longtime User
Just to be clear, so any subroutine called by Activity_Create will also complete before Activity_Pause runs even if that sub reads in a thousand bytes of data from the SD card.
 

corwin42

Expert
Licensed User
Longtime User
Just to be clear, so any subroutine called by Activity_Create will also complete before Activity_Pause runs even if that sub reads in a thousand bytes of data from the SD card.
Yes.

But you shouldn't do this. Only do necessary initialization in Activity_Create and do not load large amount of data. If Activity_Create does not return after a few seconds you will get an ANR (Activity Not Responding) error. Load your data asynchronously.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…