Peeyush Bhatnagar
Peeyush Bhatnagar

Reputation: 11

Disable Back Key on a particular event on a particular Activity

Can we disable the back key in Android on a particular screen(Activity) when a particular event happen on that screen. The case is like :- On a screen showing some records on listview, if a refresh button is pressed, an activity indicator appears on screen. At this time we have to disable the back key.

Upvotes: 1

Views: 152

Answers (4)

wSakly
wSakly

Reputation: 397

You can use this for your case

@Override
public void onBackPressed() {

    if (myCase) {
        // add your specific treatment
    } else {
        // the normal case the BackPressed
        super.onBackPressed();
    }
}

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

If looking for android api level upto 1.6.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK) {
         //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
         return true;
         }
         return super.onKeyDown(keyCode, event);    
    }

And if looking for a higher api level 2.0 and above this will work great

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

Upvotes: 2

Vikrant_Dev
Vikrant_Dev

Reputation: 390

You have to Override the onBackPressed method and leave it blank.

@Override
public void onBackPressed () {
    // Do nothing here.
}

If you leave the method unimplemented, then it will disable the Back key. You can write any code inside this method to do any task you want according to your logic.

Upvotes: 0

Razgriz
Razgriz

Reputation: 7343

Use this:

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

    //do what you want to do here.

}

Upvotes: 0

Related Questions