4

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 ?

Alamri
  • 2,112
  • 2
  • 16
  • 21

4 Answers4

5

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

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Selected as the correct answer for supporting all versions & timimg of the answer. thank you.. – Alamri Jun 26 '13 at 04:53
3

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();       

    }

}
Harshid
  • 5,701
  • 4
  • 37
  • 50
1

If you want to disable backbutton for certain time use this,

//for 5 sec = 5000
countDownTimer = new CountDownTimer(5000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            txtWait.setTextColor(getResources().getColor(R.color.errorcolor));
            txtWait.setText("Wait( " + millisUntilFinished / 1000+" sec)");
            onBackPressed();
        }

        @Override
        public void onFinish() {
            YourActivityName.super.onBackPressed();

        }
    }.start();

And in the override method:

     @Override
    public void onBackPressed() {

        //super.onBackPressed();  commented this to disable the back press
    }
Sohel Mahmud
  • 187
  • 12
0

Full working code:

YourActivity extends AppCompatActivity{

   boolean isBackButtonDisabled = false;

   @Override
   public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState)
         setContentView(R.layout.somelayout);

         disableBackButton(4000);    //<--Back button is disabled

   }


   @Override
   public void onBackPressed(){

        if(!sBackButtonDisabled){
              super.onBackPressed();
        }
   }

   private void disableBackButton(final int timeInMilis){

    if(!isBackButtonDisabled) {

        isBackButtonDisabled = true;             //<-- Keep it outside Thread code 

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(timeInMilis);
                } catch (InterruptedException e) {

                } finally {
                    isBackButtonDisabled = false;
                }
            }
        });
        t.start();
    }
}

}

Note: You can use disableBackButton(time) in other scenarios as well. For example Button click. If you click button multiple times the Thread will only run once. Because in this code isBackButtonDisable variable is thread-safe "in a way".

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88