Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable back button for temporary time

Tags:

android

I know this question have been asked many times around here, but i didn't find the propert answer for my issue.

this code can disable back button:

@Override
public void onBackPressed() {
    // Do Here what ever you want do on back press;
}

but is there anyway that i can disable back button for a temporary time,not for the whole Activity ?

like image 981
Alamri Avatar asked Nov 29 '22 01:11

Alamri


2 Answers

nice answer by Dixit. Just another option

public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean result = false;
    if (keyCode == KEYCODE_BACK) {
        if (condition) {
            result =  true;
        }
    }
    return result;
}

N.B ..

  • it will work on ancient version also

  • returning true from onKeyDown consumes the default behavior

like image 176
stinepike Avatar answered Dec 16 '22 00:12

stinepike


You have to set on boolean flag where you have to require disable back button set flag value true;

In onBackPressed() you have to put condition as per @Dixit says

@Override
   public void onBackPressed() {

    if(condition to check){
       // this block disable back button

    }else{
       // this block enable back button
        super.onBackPressed();       

    }

}
like image 39
Harshid Avatar answered Dec 16 '22 00:12

Harshid