Why is oncreate always executed when moving between activities?
I have doubts about the activity lifecycle.
I have two activities in which I keep track of all the lifecycle callback methods. If I go from activity1 to activity2, go back to activity1 and so on, with startActivity (intent) on onclick buttons, I can see that oncreate is always executed for both activities, but ondestroy is not executed.
I am switching from one operation to another:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Activity2.class);
startActivity(intent);
}
});
This is what I cheat in the log
When the app is running:
09-23 23:01:03.863 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onCreate 09-23 23:01:03.951 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onStart 09-23 23:01:03.953 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onPostCreate 09-23 23:01:03.953 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onResume 09-23 23:01:03.960 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onPostResume
Press the button to go to step 2:
09-23 23:01:46.722 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onPause 09-23 23:01:46.758 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onCreate 09-23 23:01:46.848 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onStart 09-23 23:01:46.854 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onPostCreate 09-23 23:01:46.855 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onResume 09-23 23:01:46.855 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onPostResume 09-23 23:01:47.114 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onSaveInstanceState 09-23 23:01:47.155 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onStop
Press the button to go to step 1:
09-23 23:02:08.156 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onPause 09-23 23:02:08.372 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onCreate 09-23 23:02:08.683 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onStart 09-23 23:02:08.683 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onPostCreate 09-23 23:02:08.683 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onResume 09-23 23:02:08.684 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity1 onPostResume 09-23 23:02:09.078 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onSaveInstanceState 09-23 23:02:09.088 2485-2485/com.qqapps.androidactivitylifecycle I/Test? Activity2 onStop
You can see Activity1's onCreate is executing when I go back to it.
The same happens when I define activity1 as parent of activity2 with this in AndroidManifest.xml
<activity
android:name=".Activity1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="@string/title_activity_activity2"
android:parentActivityName=".Activity1" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.qqapps.androidactivityparent.Activity1" />
</activity>
When I go back to the activity button using the option in the activity bar, oncreate is excluded for activity1.
This is what I cheat in the log
When the app is running:
09-25 15:36:17.269 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onCreate 09-25 15:36:17.469 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onStart 09-25 15:36:17.469 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onPostCreate 09-25 15:36:17.470 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onResume 09-25 15:36:17.470 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onPostResume
Press the button to go to step 2:
09-25 15:36:49.210 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onPause 09-25 15:36:49.224 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onCreate 09-25 15:36:49.253 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onStart 09-25 15:36:49.253 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onPostCreate 09-25 15:36:49.254 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onResume 09-25 15:36:49.255 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onPostResume 09-25 15:36:49.528 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onSaveInstanceState 09-25 15:36:49.566 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onStop
Coming back to activity1 from the action bar:
09-25 15:37:17.741 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onPause 09-25 15:37:17.754 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onDestroy 09-25 15:37:17.765 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onCreate 09-25 15:37:17.790 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onStart 09-25 15:37:17.790 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onPostCreate 09-25 15:37:17.791 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onResume 09-25 15:37:17.792 5208-5208/com.qqapps.androidactivityparent I/Test? Activity1 onPostResume 09-25 15:37:17.957 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onStop 09-25 15:37:17.958 5208-5208/com.qqapps.androidactivityparent I/Test? Activity2 onDestroy
In this case, I can see that onDestroy is called for both actions when the action bar button is clicked. I think it's extraneous.
As I see here http://developer.android.com/training/basics/activity-lifecycle/index.html I think oncreate is only called when the app starts or when the system has shutdown the app, but I can't see the calls to ondestroy.
thank
source to share
If you use buttons to navigate from activity1 to activity2 and from Activity2 to Activity1, you will keep creating new activities and just create a big heap in the activity stack in your application. Then you will see what onCreate()
gets called every time you switch, and you never see onDestroy()
what gets called because none of your actions get destroyed. They are all on the action stack in your task. You can even see them all if you use the command line tool adb shell dumpsys activity activities
.
Also, as I pointed out in my comment, when you return from activity2 to activity1 using a button in the action bar, you are using "navigation up". There are special rules for processing: See http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp
source to share