4

I'm trying to implement ActionBar UP navigation based on the Android design guidelines ( http://developer.android.com/design/patterns/navigation.html ).

For most use cases, it's trivial, but suppose that I have an Activity called Activity2A, that my be reached on 2 distinct paths:

MainActivity -> Activity1A -> Activity2A

MainActivity -> Activity1B -> Activity2A

The logical parent of Activity2A is Activity1A, so that should be started upon UP navigation from Activity2A. I've set this in the manifest based on the official tutorial:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock.Light" >

    <!-- Main menu -->
    <activity
        android:name="hu.scythe.upnavigation.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Level 1 -->
    <activity
        android:name="hu.scythe.upnavigation.Activity1A"
        android:label="@string/title_activity_activity1"
        android:parentActivityName="hu.scythe.upnavigation.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="hu.scythe.upnavigation.MainActivity" />
    </activity>
    <activity
        android:name="hu.scythe.upnavigation.Activity1B"
        android:label="@string/title_activity_activity1_b"
        android:parentActivityName="hu.scythe.upnavigation.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="hu.scythe.upnavigation.MainActivity" />
    </activity>

    <!-- Level 2 -->
    <activity
        android:name="hu.scythe.upnavigation.Activity2A"
        android:label="@string/title_activity_activity2"
        android:parentActivityName="hu.scythe.upnavigation.Activity1A" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="hu.scythe.upnavigation.Activity1A" />
    </activity>
</application>

I'm using the following code in Activity2A for up navigation:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // This activity is NOT part of this app's task, so create a new
                // task
                // when navigating up, with a synthesized back stack.
                TaskStackBuilder.create(this)
                // Add all of this activity's parents to the back stack
                        .addNextIntentWithParentStack(upIntent)
                        // Navigate up to the closest parent
                        .startActivities();
            } else {
                // This activity is part of this app's task, so simply
                // navigate up to the logical parent activity.
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

It's working fine if I navigate to Activity2A on the first path (so it's logical parent is on the Activity stack already). But using the second path, clicking on the UP button takes me to MainActivity (the root), not to Activity1A (the logical parent). I'm testing this code on Android 4.2.

What am I doing wrong?

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101

0 Answers0