Last active
April 24, 2021 18:31
-
-
Save WilderMinds/52ab7f4c7d7aa0b28a8438f1d7b47e45 to your computer and use it in GitHub Desktop.
Navigating backwards when implementing the CustomNavigator
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
class RandomViewsFragment : Fragment() { | |
private val customNavigator = CustomNavigator() | |
/** | |
* Intercept onBackPressed | |
*/ | |
private val onBackPressedCallback : OnBackPressedCallback = object : OnBackPressedCallback(true){ | |
override fun handleOnBackPressed() { | |
back() | |
} | |
} | |
/** | |
* Register onBackPressedCallback | |
*/ | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressedCallback) | |
} | |
/** | |
* Navigate backwards | |
* | |
* Hand navigation control back to the NavController when there | |
* are no more custom views | |
*/ | |
private fun back() { | |
val success = customNavigator.back(); | |
if (!success){ | |
findNavController().popBackStack() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment