So I have:
Fragment A: here there is a viewPager2 and TabLayout with 2 tabs, Fragment B and C.
Then inside Fragment B , I have a button that goes to a Fragment D outside of the TabLayout
, there I want get some data, go back and populate a TextView
on Fragment B in the TabLayout
in Fragment A.
It works fine until I am on Fragment D and try to get back to Fragment B and get Fragment no longer exists for key f#0
How can I fix this?
Fragment A TabLayout
Adapter:
private inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
override fun getItemCount(): Int = 2
override fun createFragment(position: Int): Fragment {
return if (position == 0) FragmentB()
else FragmentC()
}
}
Fragment B:
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<String>("key")?.observe(
viewLifecycleOwner) { result ->
binding.textView.text = result
}
Fragment D:
fun onGoBack(data: String){
findNavController().apply {
previousBackStackEntry?.savedStateHandle?.set("key", data)
navigateUp()
}
}
MainActivity onSupportNavigateUp
:
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
NavGraph:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.example.FragmentA"
android:label="Fragment A" />
<fragment
android:id="@+id/fragmentB"
android:name="com.example.FragmentB"
android:label="FragmentB" />
<fragment
android:id="@+id/fragmentC"
android:name="com.example.FragmentC"
android:label="FragmentC" />
<fragment
android:id="@+id/fragmentD"
android:name="com.example.FragmentD"
android:label="FragmentD" />
</navigation>