6.3 C
New York
Sunday, March 26, 2023

Disable Back Button Press

How to Disable Back Button Press in Android

This tutorial explains step by step how to disable back button in Android Application. This is one of the most needed things as you want to continue doing the work without any disturbance or interruption.

In Android we can change the default behaviour of the any activity like keypress, back button press, swap left swap right, to do Disable Back Press we need to override default functionality of onBackPressed() or onKeyDown() method in Activity class.

Please follow the steps below in order disable back button in Android Application:

By Using onBackPressed()

1.Open Android Studio and go to MainActivity.java. Nothing has to be done in the Activity Main.

2.Enter a code in the Override method. The code being

public void onBackPressed()

package com.mylockscreen;
public class MainActivity extends AppCompatActivity

  protected void onCreate (Bundle savedInstanceSta
        super.onCreate (savedInstanceState);

   public void onBackPressed () {


3.You will find there is no Back button working from your Android App.


By Using onKeyDown()

In this process when someone press back button we override the default behavior and execute our custom function 

Open your Activity class

public boolean onKeyDown(int key_code, KeyEvent key_event) {
    if (key_code== KeyEvent.KEYCODE_BACK) {
        super.onKeyDown(key_code, key_event);
        return true;
    return false;


In this way you can Disable Back Button Press in Android

Related Articles

Latest Articles