I am trying to implement ActionBar UP navigation based on Android development guidelines ( http://developer.android.com/design/patterns/navigation.html ).
In most cases, this is trivial, but suppose I have an Activity called Activity2A, that I have to be achieved in two different ways:
MainActivity β Activity1A β Activity2A
MainActivity β Activity1B β Activity2A
The logical parent of Activity2A is Activity1A, so it must be started when navigating UP from Activity2A. I 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" > <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> <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> <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 use the following code in Activity2A for navigation:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar 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 task, so create a new // task // when navigating up, with a synthesized back stack. TaskStackBuilder.create(this) // Add all of this activity parents to the back stack .addNextIntentWithParentStack(upIntent) // Navigate up to the closest parent .startActivities(); } else { // This activity is part of this app task, so simply // navigate up to the logical parent activity. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); }
It works fine if I go to Activity2A on the first path (so the logical parent is already on the activity stack). But using the second path, clicking on the UP button leads me to MainActivity (root), and not to Activity1A (logical parent). I am testing this code on Android 4.2.
What am I doing wrong?
source share