When a user presses back button to Disable Back Button in android and you want to perform any of the action instead of exit from the app. Like you have seen in many apps when a user presses back button to exit an app a toast message is displayed with message “press again to exit” or a dialog is displayed to “rate the app”. It’s very easy to Disable Back Button in Android with functionality and write your own ones.

Disable Back Button in Android
All you need is to implement onBackPressed() function and Override this function, it will result like below

override fun onBackPressed() {
	super.onBackPressed()
}

now just remove super.onBackPressed() and write your own code which you want to run when a user presses the back button.

override fun onBackPressed() {
	// do something
}

Please feel free to comment if you have newer techniques as well as ask questions. And, yeah! If this post helps you please do share!

Happy coding!


Leave a Reply

Your email address will not be published. Required fields are marked *