Last active
February 7, 2022 09:44
-
-
Save wajahatkarim3/2086a5d0bc04004517e4212aafcb3407 to your computer and use it in GitHub Desktop.
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 characters
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