28

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 ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
jsstp24n5
  • 594
  • 1
  • 6
  • 13

3 Answers3

28

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

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • 9
    What happens if the Process was not killed only activities were destroyed (Assuming some service was running) . When Activity starts again, will the static variable exist with last stored value ? – Ahmed Nov 15 '12 at 20:49
  • 2
    @jane static variables will be maintained if process is not killed – nandeesh Nov 16 '12 at 08:08
  • 2
    @Ahmed yes, the static variables remain there. – suitianshi Jan 29 '14 at 03:26
  • @nandeesh What about the static variable of a new class? I am setting a static variable of another class in Activity1 and after that I am switching to Activity2. When I print the value in the new activity, I don't see the value that I've set. It takes the default value. Based on the default behaviour of Static variables in Java, I would say this is not the intended behaviour. So ,I wonder what's happening here. – Durga Swaroop Dec 19 '16 at 17:42
10

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.

agamov
  • 4,407
  • 1
  • 27
  • 31
7

The answer is (3). If you need to keep values, persist them in shared preferences when each activity pauses and restore them when it resumes. Alternatively, you can also maintain an "initialized" static flag and re-initialize the static variables from any activity's onCreate() method if it is false.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @Criticalquestionnaire - I'm not sure what you're looking for, but see the guide topic on [Storage Options](http://developer.android.com/guide/topics/data/data-storage.html) for how to persist data. – Ted Hopp Jan 19 '13 at 23:22