簡體   English   中英

為什么僅在一項活動中啟用了向上按鈕,而所有活動中都添加了該按鈕?

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

我跟隨Google文檔添加了向上按鈕,這是鏈接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個子活動,之后我通過在Java文件中添加以下代碼行,選擇了NumbersActivity來測試向上按鈕:

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

使向上按鈕可用於所有子活動,以返回到父活動。

注意:這種返回上級活動的方式始終會重新創建上級活動,而這可能不是您想要的。
因此,為避免重新創建父活動,請在清單中為MainActivity添加以下行:

android:launchMode="singleTop"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM