8

I'm currently migrating my application to androidX and the new navigation component. My app has different settings screens which are nested into each other. Before androidX the PreferenceScreen were nested into each other. Now days this is handled as:

<Preference
    app:fragment="com.example.SyncFragment"
    .../>

So in XML we give an absolute class name for the PreferenceFragmentCompat we want to show by clicking on that preference. The fragment transaction has to be implemented by the Activity, which holds the fragments. I would love to do this by my NavControler but here is my problem:

override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
    val nav = Navigation.findNavController(this, R.id.nav_host_fragment)
    nav.navigate(<Fragment ID>) // Only int IDs can be passed here
    return true
}

Is there any way I could give the pref reference, which I get through this callback, an ID so I can handle the fragment transaction with the NavControler? Or do I over complicate things here and have missed something?

Cilenco
  • 6,951
  • 17
  • 72
  • 152

0 Answers0