Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.