Online Tutorials & Training Materials | STechies.com
Register Login

Disable Back Button Press

|| 0

Disable Back Button Press
Stechies

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;
import...
public class MainActivity extends AppCompatActivity

  @Overrade
  protected void onCreate (Bundle savedInstanceSta
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

   @Overrade
   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

@Override
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