I need to find out from which fragment the user switched to another fragment. I thought I should use the fragment name, but how do I get that name? Or is my method not very reliable and there are some other options?
P.S.: I use NavHostFragment
and Kotlin
CodePudding user response:
you can add OnDestinationChangedListener to your NavController to track the fragment changes.
val navhostContainer = supportFragmentManager.findFragmentById(R.id.nav_host_container)
controller = navhostContainer!!.findNavController()
controller.addOnDestinationChangedListener(listener)
val listener = NavController.OnDestinationChangedListener{
controller, destination, arguments ->
println("Destination Observe: ${destination.label}")
}
When the fragment is changed, listener will be triggered.
I hope that solution helps to solve your issue.
CodePudding user response:
You'd consider passing some data between destinations/fragments; for instant you would consider the passed-in data to be integers; and create constant integers for each fragment, whenever you do a navigation, then pass the constant assigned for the current fragment.
Example:
By using safe Args for actions & passing data during the navigation.
Assuming we want to navigate from FragmentA
to FragmentB
:
The NavGraph
should have an action represents the direction and an argument to be set to the origin fragment during the navigation:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@ id/fragmentA"
android:name="......FragmentA"
android:label="....">
<action
android:id="@ id/action_fragment_a_to_fragment_b"
app:destination="@id/fragmentB" />
<argument
android:name="previousFragment"
android:defaultValue="-1"
app:argType="integer" />
</fragment>
<fragment
android:id="@ id/fragmentB"
android:name="......FragmentB"
android:label="....">
<argument
android:name="previousFragment"
android:defaultValue="-1"
app:argType="integer" />
</fragment>
</navigation>
Create the constants that correspond to destination fragments:
companion object {
const val FRAGMENT_A = 101
const val FRAGMENT_B = 102
}
And the navigation action from FragmentA to FragmentB:
Do the navigation @FragmentA
:
findNavController().navigate(
FragmentADirections.actionFragmentAToFragmentB()
.setPreviousFragment(FRAGMENT_A)
Then check that @FragmentB
:
val args: Fragment1_2Args = Fragment1_2Args.fromBundle(requireArguments())
when (val previousFragment = args.previousFragment) {
FRAGMENT_A -> {
Log.d("LOG_TAG", "$previousFragment") // Should print 101
}
// Complete the rest of fragment cases
}