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:
Will variable "foo" at this point have the value that was set to it when Activity A's onCreate() method ran?
Variable "foo" does not exist ?
Variable "foo" exists and but is now the initialized value and not the value set in Activity A's onCreate() method ?
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.
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.
onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.
onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With