3

I have a doubt about activity lifecycle.

I have two activities in wich I trace all lifecycle callback methods. If I go from activity1 to activity2, return to activity1 and so on, with startActivity(intent) at buttons onclick, I see that oncreate is always executed for both activities, but no ondestroy have been executed.

I change fromone activity to another whith this:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), Activity2.class);
        startActivity(intent);
    }
});

This is what I obtaing at log

When the app is launched:

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

When clicking the button to go to activity2:

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

When clicking the button to go to activity1:

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 that Activity1 onCreate is executed when I return to it.

The same thing happens when I define activity1 as parent of activity2 with this at 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 return to activity button using the option at the action bar, oncreate is excecuted for activity1.

This is what I obtaing at log

When the app is launched:

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

When clicking the button to go to activity2:

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

When returning 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 see that onDestroy is called for both activities when clicking the action bar option. I think this is extrange.

As I see here http://developer.android.com/training/basics/activity-lifecycle/index.html I think oncreate only is only called when the app is launched or when de system has destoyed the app, but I don't see ondestroy calls.

Thanks

elamas
  • 211
  • 3
  • 8
  • 2
    onCreate is called everytime ativity is created, method startActivity(intent,class) creates new activity, your button is creating new activity, never returning to it, if you want to return from activity2 to activity one, just hit back button on your device – Marcel Krivek Oct 21 '14 at 14:20
  • 2
    @user2478830 To elaborate on Marcel a bit further (he is correct), the `onDestroy()` is called by the system and you typically don't have control over that, so it cannot be used as a reliable call. If you want a single instance of an Activity, there are flags that you can set on those activities to get that kind of nature. – Jay Snayder Oct 21 '14 at 14:24
  • 2
    I agree with @MarcelKrivek, Use one activity as your "`MAIN`" and the second as your secondary, always launching the secondary from the main, and moving back to main, closing secondary. If both needs to stay open, use the `FLAG_ACTIVITY_REORDER_TO_FRONT` flag in your intent. http://stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foreground-top-of-stack – Pierre Oct 21 '14 at 14:24
  • what will happen if you press back button from your device , it will Resume dont start from beginning ? but when you intent fired it recreate the other activity this why onCreate called – Mina Fawzy Oct 21 '14 at 14:34
  • 1
    When you return from activity2 to activity1 by using the button in the action bar, you are using "up navigation". There are special rules for how this is handled: See http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp – David Wasser Oct 21 '14 at 15:10

1 Answers1

1

If you use buttons to go from activity1 to activity2 and from activity2 to activity1, you will continue to create new activities and just create a big pile of them in the activity stack in your app. You will then see onCreate() being called every time you switch and you will never see onDestroy() being called, because none of your activities are being destroyed. They are all there in the activity 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've indicated in my comment, When you return from activity2 to activity1 by using the button in the action bar, you are using "up navigation". There are special rules for how this is handled: See http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

David Wasser
  • 93,459
  • 16
  • 209
  • 274