NavUtils.navigateUpTo() does not start any Activity

flx picture flx · Nov 15, 2013 · Viewed 12.5k times · Source

I have two activities

  • MainActivity
  • DeepLinkActivity

I set up everything to use the NavUtils for navigating up like advised here, here and here.

What I want to achieve is:

  1. Start DeepLinkActivity via a deep link
  2. Press up
  3. Go to MainActivity

Everything works nicely as long as there is any task of my app in the recent apps.

However, when I swipe away my app from the recent apps, it behaves like this:

  1. Swipe away my app from recent apps
  2. Start DeepLinkActivity via a deep link
  3. Press up
  4. My app closes, like when pressing back

I debugged the code, and found out, that NavUtils.shouldUpRecreateTask() returns false. upIntent has everything set to normal, like my Component set. But still, NavUtils.navigateUpTo() behaves just like a call to finish(). No log statement, nothing.

Any ideas, how to fix that?

AndroidManifest.xml

<activity
    android:name=".DeepLinkActivity"
    android:parentActivityName="my.package.MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="my.package.MainActivity"/>
    <intent-filter>
        <!-- Some intent filter -->
    </intent-filter>
</activity>

DeepLinkActivity.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // create new task
                TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                        .startActivities();
            } else {
                // Stay in same task
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

----- Edit -----

I realized that a few Google Apps are broken in the same way. If you jump e.g. to Contacts from search, press up in AB and you'll find yourself on the home screen instead of the contacts app. (API19/cm11)

Answer

Sokolov picture Sokolov · Jul 10, 2015

My solution to OPs problem:

public void navigateUp() {
    final Intent upIntent = NavUtils.getParentActivityIntent(this);
    if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
        Log.v(logTag, "Recreate back stack");
        TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
    } else {
        NavUtils.navigateUpTo(this, upIntent);
    }
}

isTaskRoot() will return true if DeepLinkActivity is the root of a task (initial launch of application or application was previously terminated through task manager). This way I'm not loosing existing back stack if activity was launched through link when applications task was already in the foreground.