Skip to content

Instantly share code, notes, and snippets.

@furkanaskin
Created April 20, 2020 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furkanaskin/36123475e2a3b8c1c102bcce68a35204 to your computer and use it in GitHub Desktop.
Save furkanaskin/36123475e2a3b8c1c102bcce68a35204 to your computer and use it in GitHub Desktop.
open class BaseBottomTabFragment : Fragment() {
var isNavigated = false
fun navigateWithAction(action: NavDirections) {
isNavigated = true
findNavController().navigate(action)
}
fun navigate(resId: Int) {
isNavigated = true
findNavController().navigate(resId)
}
override fun onDestroyView() {
super.onDestroyView()
if (!isNavigated)
requireActivity().onBackPressedDispatcher.addCallback(this) {
val navController = findNavController()
if (navController.currentBackStackEntry?.destination?.id != null) {
findNavController().popBackStackAllInstances(
navController.currentBackStackEntry?.destination?.id!!,
true
)
} else
navController.popBackStack()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment