0

Everything is working, except that I am not able to go back to the settings fragment. Why is the Fragment not called, when clicking on the back button?

Structure:

MainActivity -> SettingsFragment (inside NavController) -> Preferences Overview -> First Preference Category

SettingsFragment.java

public class SettingsFragment extends Fragment implements
    PreferenceFragmentCompat.OnPreferenceStartFragmentCallback{

private SettingsViewModel settingsViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    settingsViewModel = ViewModelProviders.of(this).get(SettingsViewModel.class);
    final View root = inflater.inflate(R.layout.settings_activity, container, false);

    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.settings, new SetupSettingsFragment(), "SetupSettingsFragment")
            .addToBackStack(null)
            .commit();

    return root;
}

public static class SetupSettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences_overview, rootKey);
    }
}

@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {

    final Bundle args = pref.getExtras();
    final Fragment fragment = getActivity().getSupportFragmentManager().getFragmentFactory().instantiate(
            getActivity().getClassLoader(),
            pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);

    int setupSettingsFragment = getFragmentManager().findFragmentByTag("SetupSettingsFragment").getId();

   getActivity().getSupportFragmentManager().beginTransaction()
            .replace(setupSettingsFragment, fragment)
            .addToBackStack(null)
            .commit();

    return true;
}

FirstPreferenceCategory.java

public class FirstPreferenceCategory extends PreferenceFragmentCompat {

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

}

PreferenceOverview.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferenceScreenOverview"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference  
        app:title="FirstPreferenceCategory"
        app:fragment="com.ui.FirstPreferenceCategory"/>

Edit:

I also moved the onPreferenceStartManager and the SetupSettingsFragment to the MainActivity, but still there is something wrong with the back stack.

Noiling
  • 87
  • 9
  • Why do you have both the `SettingsFragment` and `SetupSettingsFragment` both and not only one. I saw that you immediately launched `SetupSettingsFragment` inside the `onCreateView()` of `SettingsFragment`. Other than that, you created and initialized `SettingsViewModel` instance which I do not see being used. – cgb_pandey May 29 '20 at 07:20
  • Thank you for your answer. Your first point is correct, I will change this. I am not aware how to use the SettingsViewModel (its only included as its predefined). However, will any of this solve my problem? I am now listening to the back button and call the appropriate fragment. – Noiling Jun 06 '20 at 13:44
  • 1
    Rather than creating new instance of `FragmentThatYouWantedToGoBackTo` and starting a new `Transaction`, call `requireActivity().getSupportFragmentManager().popBackStackImmediate();` if you had added the `transaction` to `backstack` previously. – cgb_pandey Jun 06 '20 at 14:08

0 Answers0