How to disable back button in android

App Dev Insights
1 min readApr 30, 2018

--

When user presses 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 functionality and write your own ones. 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

}

--

--