Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is onDestroy always called when returning to parent activity?

Tags:

I have a very simple app based on the Building Your First App tutorial. There are two activities: MainActivity invokes DisplayMessageActivity through startActivity().

When entering DisplayMessageActivity, I see:

MainActivity.onStop() 

as expected, but when I press the back button to return to the parent MainActivity, I get:

MainActivity.onDestroy() MainActivity.onCreate(null) MainActivity.onStart() 

The activity always gets destroyed for this very simple application. But according to the documentation (second bullet point), the typical behavior is for the activity to be stopped and restarted in such cases.

Also, onDestroy() does not happen when first starting the child activity, but only once back button is clicked.

Two questions:

  1. Is there a way to prevent parent from being destroyed in the common case?
  2. Why is null being passed to onCreate() here? This prevents me from preserving state through onSaveInstanceState().

Note that I've verified that Settings -> Developer Options -> Apps -> Don't keep activities is unchecked.

Edit:

Here is how the child activity is linked to parent:

    <activity         android:name="com.example.helloworld.DisplayMessageActivity"         android:label="@string/title_activity_display_message"         android:parentActivityName="com.example.helloworld.MainActivity" >         <meta-data             android:name="android.support.PARENT_ACTIVITY"             android:value="com.example.helloworld.MainActivity" />     </activity> 

Tracing through DisplayMessageActivity.onOptionsItemSelected(), I can see that it's calling Activity.onNavigateUp().

like image 952
mtoossi Avatar asked Dec 28 '13 20:12

mtoossi


People also ask

Why onDestroy is called?

onDestroy( ) is called before the activity is destroyed.

Why onDestroy is not called?

onDestroy() will not be called for FullScreenFragment1 This is because onStart() not called for fragments in backstack and variable isStateSaved will be true.

Why is it necessary to use the onDestroy method?

onDestroy() is a method called by the framework when your activity is closing down. It is called to allow your activity to do any shut-down operations it may wish to do.

When only onDestroy is called for an activity without onPause () and onStop?

onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.


1 Answers

Thanks to Greg Giacovelli's comments, I found the answer here. The solution was to set android:launchMode="singleTop" to the parent activity.

I still can't understand why such basic information is so unknown and hard to find!

like image 75
mtoossi Avatar answered Oct 21 '22 01:10

mtoossi