操作栏UP导航困惑

时间:2013-07-16 11:45:10

标签: android android-actionbar android-support-library

我正在尝试根据Android设计指南(http://developer.android.com/design/patterns/navigation.html)实施ActionBar UP导航。

对于大多数用例来说,这很简单,但假设我有一个名为Activity2A的Activity,我可以通过2条不同的路径访问:

MainActivity - > Activity1A - > Activity2A

MainActivity - > Activity1B - > Activity2A

Activity2A的逻辑父级是Activity1A,因此应该在从Activity2A进行UP导航时启动。我根据官方教程在清单中设置了这个:

<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>

我在Activity2A中使用以下代码进行导航:

@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);
    }

如果我在第一条路径上导航到Activity2A(因此它的逻辑父元素已经在Activity堆栈上),它工作正常。但是使用第二个路径,单击UP按钮将我带到MainActivity(根),而不是Activity1A(逻辑父)。我正在Android 4.2上测试此代码。

我做错了什么?

0 个答案:

没有答案