I was very puzzled by this life cycle, so I did a little experiment. In short: the result shows that when a process is created after its destruction, the user interface objects selected in the last session have disappeared and need to be restored (which is expected). But another memory space allocated in the last session is still available for this session.
For me, the surprise is: the objects of the system UI (for example, ListView) and the memory space allocated by me are not destroyed at the same time. Why don't they die (or survive) at the same time?
Look here:
public class PracticeActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // If there is left-over value in G.count[99], then do not populate // the ListView. if (G.count[99] == 0) { ListView lv = getListView(); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_Starbucks)); } Log.d("TAG", MessageFormat.format("Created, count = {0,number}", G.count[99])); Log.d("TAG", MessageFormat.format("Starbucks = {0}", m_Starbucks[0])); G.count[99]++; // increment the count m_Starbucks[0] = "Coffee Frappuccino"; // and change the menu } @Override public void onRestart() { super.onRestart(); Log.d("TAG", "Restarted"); } @Override public void onStart() { super.onStart(); Log.d("TAG", "Started"); } @Override public void onResume() { super.onResume(); Log.d("TAG", "Resumed"); } @Override public void onPause() { super.onPause(); Log.d("TAG", "Paused"); } @Override public void onStop() { super.onStop(); Log.d("TAG", "Stopped"); } @Override public void onDestroy() { super.onDestroy(); if (isFinishing()) Log.d("TAG", "Destroyed -- someone finished me"); else Log.d("TAG", "Destroyed -- system needs resources"); } private static final String[] m_Starbucks = { "Latte", "Cappuccino", "Caramel Macchiato", "Americano", "Mocha", "White Mocha", "Mocha Valencia", "Cinnamon Spice Mocha", "Toffee Nut Latte", "Espresso", "Espresso Macchiato", "Espresso Con Panna" }; }
Here is the class G defined in the G.java file:
public class G { public static int[] count = new int[100]; }
Running this test gave the following results:
Created, count = 0 Starbucks = Latte Started Resumed Paused Stopped Destroyed
In the first session, the value of count [99] was 0, so the program switched to populating the ListView, so everything was in order.
In the second session, the counter [99] still stores the value remaining from the first session, so the program did not populate the ListView, in the hope that the ListView will also be available. But this is not so, the result is a black screen. This means that G.count [] was saved (and therefore m_Starbucks []) from the last session, but the ListView was not saved.
Obviously, in this system there is only one instance of PracticeActivity, when this instance dies, both PracticeActivity and G must die too. But they didnβt do this; they still retain values ββfrom the last session.
QUESTIONS:
- If count [] and m_Starbucks [] are still available, then that means PracticeActivity and G - both live too. Then why did the ListView pass? Do not everyone die or live at the same time?
- When I see some of my classes' members adhere to their old values ββfrom the last session, can I assume that all members of my classes are also valid ??? Ie, does Android kill my resources in an all or worthless mod? Or can he remove some and leave others? (This question should not have existed in the first place, but seeing the result of the experience, one begins to be surprised.)
Can anyone shed some light on this? Very much appreciated.