Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public static variables and Android activity life cycle management

According to the documentation the Android OS can kill the activity at the rear of the backstack.

So, say for example I have an app and open the Main Activity (let's call it Activity A). In this public activity class I declare and initialize a public static variable (let's call it "foo"). In Activity A's onCreate() method I then change the value of "foo." From Activity A the user starts another activity within my app called Activity B. Variable "foo" is used in Activity B. Activity B is then paused after the user navigates to some other activities in other apps. Eventually, after a memory shortage occurs, Activity A then Activity B can be killed. After the user navigates back to my app it restarts (actually "recreates") activity B.

What happens:

  1. Will variable "foo" at this point have the value that was set to it when Activity A's onCreate() method ran?

  2. Variable "foo" does not exist ?

  3. Variable "foo" exists and but is now the initialized value and not the value set in Activity A's onCreate() method ?

like image 936
jsstp24n5 Avatar asked Aug 30 '12 03:08

jsstp24n5


People also ask

What are the life cycle methods of Android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

What is the difference between onStop and onDestroy?

Once onStop() is called then onRestart() can be called. onDestroy() is last in the order after onStop(). onDestory() is called just before an activity is destroyed and after that it is gone it is not possible to resurrect this.

What is onStop Android?

onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.

What is onCreate method in Android Studio?

onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.


2 Answers

If the process is killed then all static variables will be reinitialized to their default values.

So whatever value you have set in Activity A will not persist

like image 187
nandeesh Avatar answered Nov 09 '22 05:11

nandeesh


Good explanation can be viewed here from 2:50 http://www.infoq.com/presentations/Android-Design

Here are some instructions for those who want to test this issue manually: Create android v.4 emulator, then go to settings -> developer settings -> disable background tasks. Then create sample android project with 2 activities, declare static variable in activity A, initialize it in onCreate() method. Place a button in activity A that starts activity B. In Activity B's onCreate() method print the value of A.staticVar to logcat.

Launch the project - activity A appears. Hit the button - activity B appears, value of static variable is printed to logcat. Press the home button and launch any other program - your sample project process will be killed (because you have disabled background processes). Now long-press on home button - you will see the list of recently launched programs. Select your sample project - OS will try to recover your project's activities back-stack and recreate last running activity B. But at this step program will crash with NullPointerException because A.staticVar will be null, and we are trying to print it to logcat.

like image 24
agamov Avatar answered Nov 09 '22 04:11

agamov