使用NavController导航到“多个首选项”屏幕时设置工具栏/ AppBar

问题描述

我有一个托管BottomNavigationView的单一活动,其中包含三个菜单选项,分别是“仪表板”,“查找”和“选项”。每个菜单选项都连接到不同的navGraph。我的活动的主题为NoActionBar

这是用于选项菜单的NavGraph-

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/options_navigation"
    app:startDestination="@id/optionsFragment">
    <fragment
        android:id="@+id/optionsFragment"
        android:name="buzz.anusmarak.ui.options.OptionsFragment"
        android:label="fragment_options" >
        <action
            android:id="@+id/action_optionsFragment_to_backupFragment"
            app:destination="@id/backupFragment" />
        <action
            android:id="@+id/action_optionsFragment_to_generalFragment"
            app:destination="@id/generalFragment" />
    </fragment>
    <fragment
        android:id="@+id/backupFragment"
        android:name="buzz.anusmarak.ui.options.BackupFragment"
        android:label="BackupFragment" />
    <fragment
        android:id="@+id/generalFragment"
        android:name="buzz.anusmarak.ui.options.GeneralFragment"
        android:label="GeneralFragment" />
</navigation>

由于我想使用PreferenceScreen,所以我用OptionsFragment扩展了BackupFragmentGeneralFragmentPreferenceFragmentCompat。这些是这三者的首选屏幕。

OptionsFragment的首选项屏幕

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference android:title="Backup"
        android:key="Backup_Preferences"
        android:icon="@drawable/ic_backup"
        app:summary="Manage Backup Account,info and more"
        app:fragment="buzz.anusmarak.ui.options.BackupFragment"/>

    <Preference android:title="General"
        android:key="General_Preferences"
        android:icon="@drawable/ic_settings"
        app:summary="Share,Rate and more"
        app:fragment="buzz.anusmarak.ui.options.GeneralFragment"/>
</PreferenceScreen>

BackupFragment的首选项屏幕

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        android:title="Backup"
        app:iconSpaceReserved="false"
        android:icon="@drawable/ic_backup">

        <CheckBoxPreference
            android:defaultValue="false"
            app:iconSpaceReserved="false"
            android:title="Cloud Backup"
            app:summary="Use Google's Firebase Cloud as backup"/>

        <SwitchPreference
            android:defaultValue="false"
            app:iconSpaceReserved="false"
            android:title="Auto Backup"
            android:summary="Automatically backup data daily at 3:00 AM" />

    </PreferenceCategory>

</PreferenceScreen>

GeneralFragment的首选项屏幕

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        android:title="General"
        app:iconSpaceReserved="false"
        android:icon="@drawable/ic_settings_color_primary">

        <Preference
            android:key=""
            android:title="Rate App"
            app:iconSpaceReserved="false"
            android:selectable="true"/>

        <Preference
            android:key=""
            android:title="Share App"
            app:iconSpaceReserved="false"
            android:selectable="true">
            <intent
                android:action="android.intent.action.VIEW"
                android:data="http://www.google.com" />
        </Preference>

    </PreferenceCategory>

</PreferenceScreen>

这就是我尝试使用GeneralFragment导航到BackupFragmentOnPreferenceStartFragmentCallback的方式-

public class OptionsFragment extends PreferenceFragmentCompat implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
    private static final String TAG = "OptionsFragment";

    public OptionsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState,String rootKey) {
        setPreferencesFromResource(R.xml.test_preferences,rootKey);
    }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller,Preference pref) {
        if (pref.getKey().equals("Backup_Preferences"))
            Navigation.findNavController(requireActivity(),R.id.fragment_container)
                    .navigate(R.id.action_optionsFragment_to_backupFragment);
        else
            Navigation.findNavController(requireActivity(),R.id.fragment_container)
                    .navigate(R.id.action_optionsFragment_to_generalFragment);

        return true;
    }
}

我收到错误消息-

java.lang.IllegalStateException:片段BackupFragment {cf5e319} (3eabfeab-b036-43a5-8e58-d1fb2ff6d7e5)id = 0x7f09012a}声明为目标 片段选项片段{e397fde} (253518d9-ba55-49fb-96be-08dfcc9af5a9)id = 0x7f09012a} 属于这个FragmentManager!

我尝试了this,但由于我不希望采取全球行动,所以它没有用。我也尝试过FragmentManager。我想做的是,在导航到不同的“首选项屏幕” 时,让OptionFragment带有

请帮忙!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...