October 14, 2017

How to disable back button in android

By Lalit Vasan


When 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 app when 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”. Its 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
    public void onBackPressed() {
         super.onBackPressed();
 }

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

 @Override
    public void onBackPressed() {
         // write your code
 }

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!

Enjoy Coding and Share Knowledge