Issue
Im using Navigation Component which navigates between fragments using findNavController().navigate(). I want to retrieve the currently displayed fragment in FragmentContainerView from MainActivity as I want to modify the "Edit" button setOnClickListener function to perform "Save" function (I already know how to change the text from Edit to Save) where it checks currently displayed fragment and change the setOnClickListener function accordingly. So in that particular fragment, it will perform "Save" function. But how do I get the currently displayed fragment from MainActivity?
I saw alot other solution but they are all using FragmentTransaction or they just show the code which get the current Fragment but they didn't show in which part of MainActivity will initiate the code which gets the current Fragment. Any help? Im using Kotlin.
Solution
You could add a OnDestinationChangedListener to the NavController inside you MainActivity
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.your_savable_fragment) {
button.text = "Save"
//....
}
if(destination.id == R.id.your_editable_fragment) {
button.text = "Edit"
//....
}
}
Answered By - avalerio

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.