Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable back button in android [duplicate]

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

like image 827
Sam Avatar asked Apr 15 '11 07:04

Sam


People also ask

Can you disable the Back button in Android?

Please navigate to Android->Advanced Restrictions->Display Settings. Uncheck the 'Hide navigation bar' option. This will disable the on-screen buttons: back, home and recent apps.

How do I get rid of the back arrow on my Android toolbar?

getActionBar(). setDisplayShowHomeEnabled(false); //disable back button getActionBar(). setHomeButtonEnabled(false); In a older android phone, the back button is removed with these two code lines.

How do I disable back press in fragment?

Here is the new way you can manage your onBackPressed() in fragment with the new call back of activity: // Disable onBack click requireActivity(). onBackPressedDispatcher. addCallback(this) { // With blank your fragment BackPressed will be disabled. }


5 Answers

@Override
public void onBackPressed() {
    // do nothing.
}
like image 137
Saurabh Pareek Avatar answered Oct 29 '22 12:10

Saurabh Pareek


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

 }
like image 36
Ali Ashraf Avatar answered Oct 29 '22 12:10

Ali Ashraf


Did you try onBackPressed() ?

@Override
public void onBackPressed() {
       // Do as you please
}
like image 34
Codemonkey Avatar answered Oct 29 '22 12:10

Codemonkey


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;
}
like image 39
Stefano Avatar answered Oct 29 '22 12:10

Stefano


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;
  }
like image 27
Petro Avatar answered Oct 29 '22 12:10

Petro