Search code examples
androidkotlinandroid-preferencesandroid-jetpack

How to navigate from preference screen to a fragment?


I am building an app where there is a settings screen which I build with Andreoid Jetpack Preference library. I want to travel from the preference screen into a more detailed fragment in order to personalize email. But I don know how to handle the click on the preference, I have managed to import the method provided from the library but I dont know how to implement it as there is no information available. I have the function onPreferenceClick but i dont know how to build its logic. It shall return false when it is not clicked and true when it is.

    override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    if (onPreferenceClick(preferenceScreen.getPreference(1))) {
        findNavController().navigate(R.id.editMailFragment)
    }

    override fun onPreferenceClick(preference: Preference?): Boolean {
    preference?.setOnPreferenceClickListener {

        }
    }
    return
}

Solution

  • I have solved it, here is the code if someone has the same trouble.

    val showValueListener = Preference.OnPreferenceClickListener {
            findNavController().navigate(R.id.editMailFragment)
            true
        }
    findPreference<Preference>("email")?.onPreferenceClickListener = showValueListener