Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – PreferenceFragments not in the same FragmentManager?

Posted on November 14, 2022

Solution 1 :

When you create a Fragment via the navigation graph, it is a child fragment of the NavHostFragment. It specifically is not the activity’s supportFragmentManager. This is why the target fragment isn’t found – you’re using the wrong FragmentManager.

However, you should not use app:fragment or onPreferenceStartFragment when you’re using Navigation. Instead, your PreferencesFragment1 should set a click listener on your Preference and have it call navigate() directly.

class PreferencesFragment1 : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences1, rootKey)
        findPreference<Preference>("pref2")?.setOnPreferenceClickListener {
            // Use whatever ID that is associated with
            // PreferenceFragment2 in your navigation graph
            findNavController().navigate(R.id.pref2)
        }
    }
}

Preferences1.xml

<?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"
    android_layout_width="match_parent"
    android_layout_height="match_parent">

    <PreferenceCategory
        android_title="Category"
        app_iconSpaceReserved="false">

        <Preference
            app_key="pref2"
            app_iconSpaceReserved="false"
            android_title="Open"/>

    </PreferenceCategory>
</PreferenceScreen>

Problem :

We have a PreferenceFragmentCompat, and with a tap on a preference, we want to switch from the current PreferenceFragmentCompat to a new PreferenceFragmentCompat. (To have certain settings on a new screen).

However, regardless of what we have tried, we keep running into the following error:

Fragment declared target fragment that does not belong to this
FragmentManager

MainActivity.kt

class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback, PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_preferences
            )
        )

        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
        val args = pref.extras
        val fragment = supportFragmentManager.fragmentFactory.instantiate(
            classLoader,
            pref.fragment)
        fragment.arguments = args
        fragment.setTargetFragment(caller, 0)
        // Replace the existing Fragment with the new Fragment
        supportFragmentManager.beginTransaction()
            .replace(R.id.nav_host_fragment, fragment)
            .addToBackStack(null)
            .commit()
        return true
    }
}

PreferenceFragement1.kt

class PreferencesFragment1 : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences1, rootKey)
    }
}

PreferenceFragment2.kt

class PreferenceFragment2 : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences2, rootKey)
    }
}

Preferences1.xml

<?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"
    android_layout_width="match_parent"
    android_layout_height="match_parent">

    <PreferenceCategory
        android_title="Category"
        app_iconSpaceReserved="false">

        <Preference
            app_key="pref2"
            app_iconSpaceReserved="false"
            android_title="Open"
            app_fragment="com.testapp.ui.preferences.Preference2"/>

    </PreferenceCategory>
</PreferenceScreen>

Preferences2.xml

<?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"
    android_layout_width="match_parent"
    android_layout_height="match_parent">
</PreferenceScreen>

The stacktrace shows the following:

java.lang.IllegalStateException: Fragment Preference2{496ebb9} (1b91d8df-58c0-485a-96f9-d392fcef528b) id=0x7f09008d} declared target fragment Preference1{b6337c1} (97aa1b0e-d5fa-4995-b0ba-e89ad0ea5ce0) id=0x7f09008d} that does not belong to this FragmentManager!
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1148)
        at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255)
        at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138)
        at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

We are using androidx.preference:preference-ktx:1.1.1.
Can some give us a concise explanation what exactly is causing this problem, and how we can solve it?

READ  [ANSWERED] java - How to do one tap signin and signup in Android
Powered by Inline Related Posts

We already looked at the following (related) posts, without any success:

  • Fragment declared target fragment that does not belong to this FragmentManager
  • How to open a new PreferenceFragment from current one, using the new Android-X API?
  • https://developer.android.com/guide/topics/ui/settings/organize-your-settings

Comments

Comment posted by developer.android.com/guide/topics/ui/settings/…

Yes, thank you for this answer! Big fan of yours by the way, as a Google employee, I wonder if the docs could be updated sometime?

Comment posted by github.com/android/user-interface-samples

I literally searching for this answer for 3 hours. I even did some research on android sample repo

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress