Last active
July 3, 2021 19:17
-
-
Save Frank1234/ccd7b49fc1186595e0577200975d254f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns true if the navigation controller is still pointing at 'this' fragment, or false if it already navigated away. | |
*/ | |
fun Fragment.mayNavigate(): Boolean { | |
val navController = findNavController() | |
val destinationIdInNavController = navController.currentDestination?.id | |
// add tag_navigation_destination_id to your ids.xml so that it's unique: | |
val destinationIdOfThisFragment = view?.getTag(R.id.tag_navigation_destination_id) ?: destinationIdInNavController | |
// check that the navigation graph is still in 'this' fragment, if not then the app already navigated: | |
if (destinationIdInNavController == destinationIdOfThisFragment) { | |
view?.setTag(R.id.tag_navigation_destination_id, destinationIdOfThisFragment) | |
return true | |
} else { | |
Log.d("FragmentExtensions", "May not navigate: current destination is not the current fragment.") | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment