Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Last active February 7, 2022 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wajahatkarim3/2086a5d0bc04004517e4212aafcb3407 to your computer and use it in GitHub Desktop.
Save wajahatkarim3/2086a5d0bc04004517e4212aafcb3407 to your computer and use it in GitHub Desktop.
private var backPressedOnce = false
override fun onBackPressed()
{
var navController = findNavController(R.id.fragNavHost)
// Check if the current destination is actually the start sestination (Home screen)
if (navController.graph.startDestination == navController.currentDestination?.id)
{
// Check if back is already pressed. If yes, then exit the app.
if (backPressedOnce)
{
super.onBackPressed()
return
}
backPressedOnce = true
Toast.makeText(this, "Press BACK again to exit", Toast.LENGTH_SHORT).show()
Handler().postDelayed(2000) {
backPressedOnce = false
}
}
else {
super.onBackPressed()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment