0

I have a Single activity hosting BottomNavigationView with three menu option as Dashboard, Find and Option. Each menu option is connected to different navGraph. My activity has a theme as NoActionBar.

This is NavGraph for Option Menu-

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

As I wanted to use PreferenceScreen so I extended OptionsFragment, BackupFragment and GeneralFragment with PreferenceFragmentCompat. These are the preferecne screen of those three-

OptionsFragment's Preference Screen

<?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's Preference Screen

<?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's Preferecnce Screen

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

This is how I tried to navigate to GeneralFragment and BackupFragment using OnPreferenceStartFragmentCallback -

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

and I am getting error as -

java.lang.IllegalStateException: Fragment BackupFragment{cf5e319} (3eabfeab-b036-43a5-8e58-d1fb2ff6d7e5) id=0x7f09012a} declared target fragment OptionsFragment{e397fde} (253518d9-ba55-49fb-96be-08dfcc9af5a9) id=0x7f09012a} that does not belong to this FragmentManager!

I have tried this but as I don't want a global action so it did not work. I have tried with FragmentManager as well. What I am trying to do is, have OptionFragment a Toolbar setup with NavGraph of OptionFragment as we navigate to different Preference Screen.

Please help !!

Nikhil Sharma
  • 897
  • 1
  • 4
  • 18

0 Answers0