Created
May 5, 2020 23:21
-
-
Save kaneoriley/b43da63ee17bd266d77228f46ce2d4c7 to your computer and use it in GitHub Desktop.
Revisions
-
kaneoriley created this gist
May 5, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ /** Workaround for 'unknown to this navigation controller' error */ fun Fragment.isCurrentDestination(): Boolean { val navController = findNavController() val currentDestinationId = navController.currentDestination?.id val fragmentDestinationId = view?.getTag(R.id.tag_navigation_destination_id) ?: currentDestinationId return if (currentDestinationId == fragmentDestinationId) { view?.setTag(R.id.tag_navigation_destination_id, fragmentDestinationId) true } else { log.error { "May not navigate: current destination is not the current fragment." } false } } fun Fragment.safeNavigate(action: NavController.() -> Unit) { if (isCurrentDestination()) { findNavController().apply { action.invoke(this) } } } ... // In the fragment: safeNavigate { // 'this' is a NavController that's able to navigate without error navigate(/** whatever overload you need */) }