Process Variables and Activity Lifecycle


Active Member
Licensed User
Hi there

I've read the tutorial on Activity Life Cycles.

Just trying to understand B4A a bit more after playing with Activities starting other activities.

Q1: Does Basic4Android store Process Variables separately from Activities?

I'm asking because I am presuming that using Activity.Finish effectively kills off an Activity but having done that, it seems that you can still access any Process Variables that were declared inside that Activity.

Q2: When you call Activity.Finish does that result in it becoming available for garbage collection thus freeing up memory?

The reason I ask is because I had an app where Activity 1 started Activity 2 which presented some UI.

The user returned to Activity 1 through selecting a ListView item which resulted in a StartActivity(Activity1). This brought back Activity1 to the foreground.

However, when I then used the Back Button in Activity 1 to close it and go back to the Home Screen, I was returned to Activity 2 - because it had never been destroyed.

So I changed my code in Activity2 to Activity.Finish and this cured the problem - no reappearance of Activity2 after closing Activity 1. The nice thing was that while Activity 1 was still alive, I could still access the process variables declared in Activity 2 which was good! :)

However, the Android documentation says about using the finish() method on an activity:

"Note: In most cases, you should not explicitly finish an activity using these methods. As discussed in the following section about the activity lifecycle, the Android system manages the life of an activity for you, so you do not need to finish your own activities. Calling these methods could adversely affect the expected user experience and should only be used when you absolutely do not want the user to return to this instance of the activity."

Q3: Given the above from the Android Docs, is it ok to use Activity.Finish in this way?

Since Basic4Android don't present the Subs onstop() and ondestroy() I am presuming it's okay to use Activity.Finish in the way I have - I can't see any other way to make sure that my second Activity does not reappear when I "back-key" out of the first one.

Bit long-winded, but hopefully you see what I am trying to get at! Just trying to change some presumptions into facts! :)



B4X founder
Staff member
Licensed User
A1: Process variables are not tied to the activities life cycle (or to any other type of module). Process variables are tied to the process life cycle. For users familiar with Java, these variables are static variables.
In fact there is no real connection between process variables and the module containing them (only logical connection).

A2, 3: Calling Activity.Finish destroys the activity. Later when you call StartActivity it is first created and then resumed. You are correct to call Finish in your case as you want to remove the activity from the activities stack.
Upvote 0


Active Member
Licensed User
Hi Erel

Excellent. Presumptions are now facts :)

Thanks for clearing those questions up so quickly.

Upvote 0