5

I'm writing a simple android application which is basically a modification of the fragments demo available in the android documentation. In the app, there is a file called Ipsum.java which has a static ArrayList of Strings called Headlines.

In the onCreate() method of the main activity, I have the following code which adds some elements into the array list.

    if (savedInstanceState == null){
        Ipsum.Headlines.add("String 1 ");
        Ipsum.Headlines.add("String 2");
    }

savedInstanceState is a Bundle that the system passes to the method, if the app is being resumed from some inactive state. The logic is that if savedInstanceState is null, then the app is not being resumed but being started as a new instance.

If I leave the app using the "Home" button and re-enter the app, the arrayList contains only the two elements: "String 1" and "String 2". (This is the desired behavior)

However, if I leave the app using the back button, and then re-enter the app, the "String 1" and "String 2" elements are added again. The array then has 4 elements.

String 1
String 2
String 1
String 2

(The contents of the arrayList can be seen because they are used to populate a listView) It seems that the app is storing the contents of the static array list when the back button is pressed.. and that a Bundle is not passed to the onCreate() method when the app is restarted. Can someone explain what's happening here, in terms of the app life cycle?

mahela007
  • 1,399
  • 4
  • 19
  • 29
  • 2
    **"if I leave the app using the back button"** One thing to understand is you don't *leave* an "app" when you press the BACK button you simply finish (terminate) the current `Activity`. If you have other application components, it's quite possible that they will remain in memory. Most importantly, something such as a `Service` can continue running and registered `BroadcastReceivers` can remain to wait for broadcasts. – Squonk Aug 05 '13 at 04:46
  • Thanks for the clarification. My App only has one activity so I assume the application will 'terminate' once the back button is pressed. However, I'm a little confused with the terminology.. Once the app is 'terminated', in which state of the life cycle will it be in? For reference: http://developer.android.com/images/training/basics/basic-lifecycle.png – mahela007 Aug 05 '13 at 08:56
  • **"I assume the application will 'terminate' once the back button is pressed."** No, not exactly. Behind the scenes there is an application framework. Even with only one `Activity`, pressing the back button won't completely unload the application from memory. Android keeps a 'skeleton' application framework in memory which makes it quicker to restart apps - it will use minimal resources but it's basically dormant. Pressing `BACK`, however, will terminate (destroy) an `Activity` so, in the link you posted, the `Activity` will be in the Destroyed state even though the application still exists. – Squonk Aug 05 '13 at 09:59

3 Answers3

12

May This Help you:

Lets start with a bit of background: What happens when you start an application?

The OS starts a process and assigns it a unique process id and allocates a process table.A process start an instance of DVM(Dalvik VM); Each application runs inside a DVM. A DVM manages class loading unloading, instance lifecycle, GC etc.

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded.

So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens:
1. the class is unloaded
2. the JVM shuts down
3. the process dies

Note that the value of the static variable will persist when you switch to a different activity of another application and none of the above three happens. Should any of the above three happen the static will lose its value.

For More Detail: Read the Answer of Samuh in this Link... Click Here

Community
  • 1
  • 1
Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
  • 1
    So does this mean that for as long as the app is not shut down by the user the static will remain loaded in the DVM? Or does OS potentially unload the DVM altogether when the Activity is paused/stopped, and thus lose static state along with it? – Marchy Oct 04 '13 at 01:48
  • Yes static variable remains till the application remains alive.. but sometimes when memory is low it might get destroyed too... There's no real way to force this. OS will unload them when it decides it needs to exit the application, generally when there are no running activities or services, or when it runs low of memory and kills a background app to claim its ram. – Bhavin Nattar Oct 04 '13 at 04:07
  • @BhavinNattar at least mention where you copied your answer from: http://stackoverflow.com/a/1944564/198996 – TomTasche Nov 21 '13 at 12:10
  • 1
    @BhavinNattar, Why do you say that the variable is loaded by the JVM? Why is there even a JVM on android? – Pacerier Nov 20 '14 at 10:47
2

Your activity is being resumed. If you want to control what happens, implement onResume().

See Managing the Activity Lifecycle for details.

EDIT:

Static variables are a Java concept. static just means that there is only one copy of the variable for the whole class. The alternative is that each object would have it's own copy.

So while your code is running, you just have one copy of the variable in your program. It doesn't get saved anyplace, unless you add code to do that.

andy256
  • 2,821
  • 2
  • 13
  • 19
  • I did read that section of the developers guide. Unfortunately, they don't say anything about static variables and their "lifetime". Thanks for the suggestion about onResume(). – mahela007 Aug 05 '13 at 04:37
0

Static variables are associated with a class and they will live as long as the class is in the memory,and destroy when class gets unloaded (which very rarely happens). It can happen when-

 -You force stop your app.
 -Application crashes.
 -You clear your app data.
 -Switch off your Device(Shutdown DVM).
T_V
  • 17,440
  • 6
  • 36
  • 48
  • If I make a variable static do I need to set it equal to null in onStop() since the class very rarely gets unloaded? I have a game and don't need resources after activity finishes. If I call finish(), what happens to static variables? – cjayem13 Aug 27 '14 at 00:47
  • yes you can set it to null in onStop() on onDestroy(). – T_V Aug 27 '14 at 06:48
  • @cjayem13 If you close the app and again open it then your static variable might be contain previous value (in case of you are not setting variable to null) – T_V Aug 27 '14 at 06:52