的向上导航预期使用情况情况

2023-09-12 02:17:10 作者:Mr.—独尊

我的应用程序是基于活性和有几个级别,即ActivityA已经开始ActivityB,这反过来又开始ActivityC动作。从ActivityC pressing'回'返回ActivtyB,并要回从返回到ActivityA,如你所愿。

My app is Activity based and has several levels, i.e. ActivityA has actions to start ActivityB, which in turn starts ActivityC. Pressing 'back' from ActivityC returns to ActivtyB, and going back from that returns to ActivityA, as you would expect.

在我的动作条我已经启用的主页图标,使其显示向上导航箭头。我的本意是在相同的方式返回按钮的工作,但点击最多的ActivityC还是回到我ActivityA,即使我已经添加了下面的清单:

In my ActionBar I've enabled the Home icon, so that it displays the Up navigation arrow. I intended it to work in the same way as the Back button, but clicking Up from ActivityC still returns me to ActivityA, even though I've added the following to the manifest:

<activity android:name=".activity.ActivityC"
    android:parentActivityName=".activity.ActivityB">
        <!-- Parent activity meta-data to support API level 7+ -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activity.ActivityB" />
</activity>

两个问题:

1)我是不是想在这里滥用的动作条?如果向上图标总是返回到主活动? 2)为什么我的实现不工作,我打算?

1) Am I trying to misuse the ActionBar here? Should the Up icon always return you to the main activity? 2) Why is my implementation not working as I intended?

推荐答案

1)不,你是不是想在这里滥用的动作条。如果您是从去 - > B - > C,那么pressing最多从C应该带你到B,不要答:这是中提到的这里为

1) No, you are not trying to misuse the ActionBar here. If you are going from A -> B -> C then pressing Up from C should take you to B and NOT A. This is mentioned here as

向上按钮用于在基于屏幕之间的层次关系的应用程序导航。举例来说,如果画面A显示项目的列表,并选择一个项目导致屏幕B(其中presents该项目更详细的),那么屏幕乙方应提供一个向上按钮返回到屏幕上。

"The Up button is used to navigate within an app based on the hierarchical relationships between screens. For instance, if screen A displays a list of items, and selecting an item leads to screen B (which presents that item in more detail), then screen B should offer an Up button that returns to screen A."

2)你的实现并不如预期,因为你忘了做提到以下的这里:

2)Your implementation is not working as intended because you forgot to do the following as mentioned here:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_displaymessage);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // If your minSdkVersion is 11 or higher, instead use:
        // getActionBar().setDisplayHomeAsUpEnabled(true);
    }

然而,我不建议你用上面的方法来处理您的向上导航作为上述方法不适用于ICS及以下工作。它不会工作的原因是因为NavUtils表现不同为pre软糖和豆形软糖后在本如此解释。

一个更好的办法是做,这是在儿童活动手动处理的后续行动:

A better way is to do this is to handle the Up action manually in the Child Activity:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            startActivity(new Intent(this, ParentActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
                          Intent.FLAG_ACTIVITY_SINGLE_TOP));
        default:
            return super.onOptionsItemSelected(item);
    }
}

和本手册的方法最好的部分是,它适用于所有的API级别。

And best part of this manual approach is that it works for ALL Api Levels.