android – Why is onDestroy always called when returning to parent activity?-ThrowExceptions

Exception or error:

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:


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


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.


Here is how the child activity is linked to parent:

        android:parentActivityName="com.example.helloworld.MainActivity" >
            android:value="com.example.helloworld.MainActivity" />

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

How to solve:

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!


Adding launchMode in Manifest changes launch mode every time, even it is not launched by child activity. There are other ways to launch existed instance.

1.override onOptionsItemSelected(item: MenuItem)

override fun onOptionsItemSelected(item: MenuItem): Boolean {
   return when (item.itemId) { -> {
            // Respond to the action bar's Up/Home button
            val upIntent: Intent? = NavUtils.getParentActivityIntent(this)
            when {
                upIntent == null -> throw IllegalStateException("No Parent Activity Intent")
                else -> {
                    //add launch flag here
                    NavUtils.navigateUpTo(this, upIntent)
        else -> super.onOptionsItemSelected(item)
  1. 2nd solution looks like a bug. Set “android:parentActivityName” to activity which is not existed in current task stack. Android will behavior like “BACK” button. It launches the top instance instead of launching parent activity. Document mentions “NavUtils.shouldUpRecreateTask” can recognize if parent activity existed in current task. But it doesn’t work in my test. Navigate up with a new back stack

Leave a Reply

Your email address will not be published. Required fields are marked *