为什么在所有活动中都添加了向上按钮,而我只在一个 activity 中启用了它?

Why The up button has been added in all activities while i enabled it only in one activity?

我按照 Google 文档添加了向上按钮,这里是 link https://developer.android.com/training/implementing-navigation/ancestral?utm_source=udacity&utm_medium=course&utm_campaign=android_basics,所以首先我修改了清单文件,使其看起来像:

?xml version="1.0" encoding="utf-8"?><!--
 Copyright (C) 2016 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
}});}}
          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.miwok">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".PhrasesActivity"
            android:label="@string/category_phrases"
            android:parentActivityName="com.example.android.miwok.MainActivity" />
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.miwok.MainActivity" />/>

        <activity android:name=".NumbersActivity"
            android:label="@string/category_numbers"
            android:parentActivityName="com.example.android.miwok.MainActivity" />
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.miwok.MainActivity" />/>

        <activity android:name=".FamilyActivity"
            android:label="@string/category_family"
            android:parentActivityName="com.example.android.miwok.MainActivity" />
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.miwok.MainActivity" />
        <activity android:name=".ColorsActivity"
            android:label="@string/category_colors"
            android:parentActivityName="com.example.android.miwok.MainActivity" />
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.miwok.MainActivity" />/>
    </application>

</manifest>

因为,你可以看到我有 4 个子活动,之后我随机选择 NumbersActivity 来测试向上按钮,方法是在它的 java 文件中添加这行代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

我无法理解的是为什么所有其他子活动都启用了向上按钮而不更新他们的 java 文件??

清单中的这些语句:

    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.android.miwok.MainActivity" />

android:parentActivityName="com.example.android.miwok.MainActivity"

使向上按钮对所有子活动可用,以便 return 对父活动 activity。

注意:这种 return 返回父级 activity 的方式总是会重新创建父级 activity,这可能不是您想要的。
因此,为了避免父 activity 的重新创建,请在 MainActivity 的清单中添加这一行:

android:launchMode="singleTop"