Reputation: 121
I have been trying to figure it this out for a while, but I seem to be stuck making headway now. Basically I am trying to understand the difference between when an Activity gets destroyed and re-created and when the process on which it runs gets destroyed and re-created.
According to the diagram here: http://developer.android.com/reference/android/app/Activity.html, it seems that Activities are re-created if and only if the underlying app process is re-created.
However, let's say I create a simple program with a single button:
public class HelloAndroidActivity extends Activity {
private int test = 15;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), test + " ", Toast.LENGTH_SHORT)
.show();
test++;
}
});
}
}
Now it will show me the numbers 15, 16, 17... when I click on the button. However, if I leave the program and go to the main menu and re-enter it, or I change the screen orientation then it re-sets to 15. So the Activity must have been re-created.
However, the program has been running all the while (as you can see when you go to Task Manager), so the process has not been destroyed.
Similarly, if I change the variable test to a static variable then it preserves its value despite the Activity being destroyed and re-created. So the process must be distinct from the Activity.
According to the diagram on the Android Activity site, the activity is only re-created when there is too little memory for the underlying process, but here it seems it is re-created in lots of other scenarios - such as screen orientations, going to the main menu, etc.
So my question is: What is the difference between the Activity lifecycle and the underlying Process lifecycle? And is there a path of Activity re-creation that is not depicted in the Android diagram that preserves the underlying process?
Thanks a lot for any help you might have with this question!
Upvotes: 0
Views: 340
Reputation: 1006869
According to the diagram here: http://developer.android.com/reference/android/app/Activity.html, it seems that Activities are re-created if and only if the underlying app process is re-created.
There are many things that will destroy an activity, including calling finish()
, the user pressing the BACK button (which, by default, triggers a call to finish()
), or the device undergoing a configuration change (which, by default, destroys and recreates the foreground activity).
However, if I leave the program and go to the main menu and re-enter it, or I change the screen orientation then it re-sets to 15. So the Activity must have been re-created.
If by "leave the program", you mean "press the BACK button", then this makes sense. Similarly, switching from portrait to landscape is a configuration changes, so that too makes sense.
However, the program has been running all the while (as you can see when you go to Task Manager), so the process has not been destroyed.
Correct.
So the process must be distinct from the Activity.
Correct. An application may have dozens or hundreds of activities, along with many other components (services, etc.). By default, they are all part of a single process.
According to the diagram on the Android Activity site, the activity is only re-created when there is too little memory for the underlying process,
No.
What is the difference between the Activity lifecycle and the underlying Process lifecycle?
The process will remain in memory until Android needs the memory for something else. Android chooses processes to terminate based on what is going on, in terms of age, active components, etc. You can read more about the process lifecycle in the documentation.
And is there a path of Activity re-creation that is not depicted in the Android diagram that preserves the underlying process?'
There are many things that destroy an activity, indicated in the diagram by the arrow leading to onDestroy()
. I enumerated several of these in the first paragraph of my answer. Once an activity is destroyed, if an instance of that activity is needed again (e.g., user re-launches the app), a fresh instance is created.
Upvotes: 1