23

In the "System Back after cross navigation to lower hierarchy levels" section of the Navigation Drawer, they say:

If the user navigates to a lower hierarchy screen from the navigation drawer and the screen has a direct parent, then the Back stack is reset and Back points to the target screen’s parent. This Back behavior is the same as when a user navigates into an app from a notification.

resetting the back stack

I know the back stack can be reset by starting an activity with FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK, but that does not seem to be usable here, as it would not create a back stack for Lower 1.1.1.

Any idea how to remove TopView2 from the stack and at the same time add the TopView1 -> Lower 1.1 back stack when starting Lower 1.1.1 ? I'm expecting a simple solution, considering this is mentioned in the Navigation Drawer document.

2
  • 1
    was my answer useful? Have you tried to implement Up Navigation? Any results or new problems? Dec 11, 2013 at 16:52
  • Seems useful - I got the idea there is something else to use besides intent flags. I did not try to implement it yet, we'll switch to it soon. The answer is very very long, a first sentence with a summary would have been useful. Thanks!
    – aleb
    Dec 12, 2013 at 8:17

1 Answer 1

63
+100

EDIT Synthesized version:

1)Declare the hierarchical navigation structure of your app in your manifest file.

2)The root activity of your app should perform a view switch between the TopViews in your app hierarchy.*

3) Activities lower in the hierarchy should perform 'Selective Up' navigation.

*Important: you should not add transactions to the back stack when the transaction is for horizontal navigation such as when switching tabs or navigation drawer top views.


Full description:

You should avoid the use of Intent Flags with the new navigation patters like Navigation Drawer for the next reasons:

  • Intent Flags are not really an API.
  • Some flags only work in exact combinations.
  • Many flags are not relevant for most 3rd party apps.
  • Overlap/conflict with activity launchMode.
  • Confusing documentation.
  • Implementation can become a process of trial and error.

Instead, opt for the new Navigation API:

  • Native Up navigation for Jelly Bean and above.
  • Based on hierarchical metadata specified for each <activity> in your manifest.
  • The support library provides equivalent functionality for earlier android versions via NavUtils.
  • TaskStackBuilder offers additional utilities for cross-task navigation.

So to answer your question the general idea is:

1) You need to declare the logical parent of each activity in your manifest file, using the android:parentActivityName attribute (and corresponding <meta-data> element) like:

<application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myapp.RootDrawerActivity" ...>
        ...
    </activity>
    <!-- A child of the root activity -->
    <activity
        android:name="com.example.myapp.Lower11 "
        android:label="@string/lower11"
        android:parentActivityName="com.example.myapp.RootDrawerActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.RootDrawerActivity" />
    </activity>
    <activity
        android:name="com.example.myapp.Lower111 "
        android:label="@string/lower111"
        android:parentActivityName="com.example.myapp.Lower11" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.Lower11" />
    </activity>
</application>

2) In your root Activity, drawer item selection should initiate a 'view switch' action by replacing the Activity's current fragment content.

A view switch follows the same basic policies as list or tab navigation in that a view switch does not create navigation history. This pattern should only be used at the root activity of a task, leaving some form of Up navigation active for activities further down the navigation hierarchy (In your case Lower 1.1 & Lower 1.1.1). The important thing here is that you don't need to remove TopView2 from the stack but to perform a view switch as commented before passing the position of the view (or fragment id) as an extra.

In your root Activity do something like this:

@Override
protected void onDrawerItemSelected(int position) {

        // Update the main content by replacing fragments
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        args.putInt(RootDrawerActivity.ARG_SORT, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, fragment).commit();

        // Update selected item and title, then close the drawer
        setDrawerItemChecked(position, true);
        setTitle(getResources().getStringArray(R.array.drawer_array)[position]);
        closeDrawer();

}

3) Then lower in the hierarchy (i.e. Lower1.1) you should perform 'Selective Up' navigation, recreating the task stack in the process.

Selective Up allows a user to jump across an app's navigation hierarchy at will. The application should treat this as it treats Up navigation from a different task, replacing the current task stack (This is what you want!) using TaskStackBuilder or similar. This is the only form of navigation drawer that should be used outside of the root activity of a task.

@Override
protected void onDrawerItemSelected(int position) {

        TaskStackBuilder.create(this)
                        .addParentStack(RootDrawerActivity.class)
                        .addNextIntent(new Intent(this, RootDrawerActivity.class)
                                        .putExtra(RootDrawerActivity.ARG_SORT, position))
                        .startActivities();

}

Refs:

http://developer.android.com/training/implementing-navigation/ancestral.html https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android

5
  • 1
    This Guy should get the reputation. Im searching for an overview like this for hours and it covers the whole topic pretty well!
    – danijoo
    Dec 6, 2013 at 13:22
  • Even here in the future this is useful. Thanks for a great reply.
    – doorstuck
    Mar 5, 2014 at 7:32
  • 1
    Good answer but this doesn't work for Android 4.4 devices. I ended up passing a boolean and then overriding the onBackPressed in the activity that was launched instead.
    – Slickelito
    Apr 16, 2014 at 8:58
  • 1
    @Slickelito Why doesn't it work for Android 4.4 devices?
    – aleb
    May 22, 2014 at 7:38
  • The speakerdeck link referenced in this answer is an absolute must read! If you are targeting API level 16 and above, most of the time you don't even need to do anything. The framework will handle everything for you. Oct 13, 2016 at 4:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.