13

i am making swap application... i want to disable back button. so i use this code... and passed intent in to it..

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

but while running when i press back button on emulator it updates activity but when i repress back button it switch to the home page of an emulator.. please suggest is their any other method

Ganapathy C
  • 5,989
  • 5
  • 42
  • 75
Sam
  • 355
  • 2
  • 4
  • 13
  • 1
    You can override either of these method `dispatchKeyEvent`, `onBackPressed`, `onKeyDown`. Refer to [this answer](https://stackoverflow.com/a/44714357/6521116) for more. – LF00 Jun 23 '17 at 07:17

7 Answers7

64
@Override
public void onBackPressed() {
    // do nothing.
}
Bikash
  • 1,452
  • 1
  • 15
  • 24
Saurabh Pareek
  • 7,126
  • 4
  • 28
  • 29
  • i just applied @Override public void onBackPressed() { // do nothing. return; } and by applying intent now i can switch to the page where i want – Sam Apr 15 '11 at 09:34
  • 2
    Has this really been up for more than two years with "do not return true" from a `void` method? Come on...there are several more complete and correct answers here. – CodeClown42 Nov 28 '13 at 13:00
16
 @Override
 public void onBackPressed() {

    //super.onBackPressed();
    if(SOME_CONDITION_TO_CHECK){
        //THIS BLOCK WILL NOT DO ANYTHING AND WOULD DISABLE BACK BUTTON             
    }else{
       super.onBackPressed();
       //THIS BLOCK WILL BE CALLED IF ABOVE COND IS FALSE, AND WOULD ENABLE BACK BUTTON
    }   

 }
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Ali Ashraf
  • 1,841
  • 2
  • 24
  • 27
6

Did you try onBackPressed() ?

@Override
public void onBackPressed() {
       // Do as you please
}
Codemonkey
  • 3,412
  • 3
  • 20
  • 24
3

This is my solution it works using Android 2.2

override onBackPressed() ...

public void onBackPressed() {
   // check if you are in the right mode 
   if (getCurrentState() == FORM_STATE_BROWSE ) {
       // ok do default back action
       super.onBackPressed();
   }

   // exit skipping "onBackPressed()"
   return;
}
Stefano
  • 291
  • 3
  • 2
1

You must add return in order for this to successfully work on all devices, my Note-3 & HTC still go back if I don't include return!

  @Override
  public void onBackPressed() {
        return;
  }
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Petro
  • 3,484
  • 3
  • 32
  • 59
  • That seems very strange because Java can see no difference between no return and return in this case. Return just exits the method doing nothing. – JohnyTex Apr 18 '16 at 15:55
  • Just posting what happened, I don't have a technical reason. – Petro Apr 19 '16 at 01:12
0

Like if you want to move on some activity by pressing back button,override this method in your activity

 @Override
    public void onBackPressed() {
        // do nothing.
        Intent intent = new Intent(SocialPlugIns.this, MainActivity.class);
        Log.i("Hello", "This is Coomon Log");
        startActivity(intent);
           return;
    }
Kirtikumar A.
  • 4,140
  • 43
  • 43
0

You have to override onBackPressed() function. Below I am giving a way:

   @Override
    public void onBackPressed() {
       //make some alert for the user
    }
thor
  • 21,418
  • 31
  • 87
  • 173
Akhter Al Amin
  • 852
  • 1
  • 11
  • 25