5

I want to block hardware back button in android ,in order to prevent from going back to other activity.. Thanks in advance...

Kakey
  • 3,936
  • 5
  • 29
  • 45
  • No reason to vote done, as the question is legitimate: You can prevent back button for dialogs, so at least when using an activity in a dialog style it may make sense to block the back button. – sven Nov 02 '10 at 13:17

2 Answers2

17

Here is code that allows you to handle the back key in an activity correctly on all versions of the platform:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (  Integer.valueOf(android.os.Build.VERSION.SDK) < 7 //Instead use android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        // Take care of calling this method on earlier versions of
        // the platform where it doesn't exist.
        onBackPressed();
    }

    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    // This will be called either automatically for you on 2.0
    // or later, or by the code above on earlier versions of the
    // platform.
    return;
}

sources:http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
100rabh
  • 6,156
  • 5
  • 27
  • 41
  • Excellent to know, especially due to Android's awkward Frgagment backstack that doesn't allow you to clear the oldest items while keeping the newest. – Peter Ajtai Nov 27 '11 at 06:58
  • @100rabh, I used your above code which worked perfectly within my GameView. However, within the View I have dialog popups. How can i implement the above disabling of back button for the popups? Thanks – SingleWave Games Jul 16 '12 at 11:43
  • @LandLPartners For dialogs you need to handle keys using [DialogInterface.OnKeyListener](http://developer.android.com/reference/android/content/DialogInterface.OnKeyListener.html) . Just catch the back key as above & you're done ! – 100rabh Jul 16 '12 at 12:35
  • @100rabh, I have implemented this using - public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent evt) and then return super.onKeyDown(keyCode, evt); However, this does not seem to work, I assume the return line of code is incorrect? – SingleWave Games Jul 16 '12 at 13:41
  • @LandLPartners try this [How to handle Back button with in the dialog?](http://stackoverflow.com/a/10346049/437703) – 100rabh Jul 17 '12 at 09:08
4

If the 'other activity' is yours, you can set it to not appear in the history list.

Otherwise, remember that the phone belongs to the user and not to you, and stop trying to tell them what they can and can't do with THEIR device.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • 2
    I think in some cases this is acceptable. I just ran into one where I have a set of preferences that automatically save when the user goes back, however in the case that they forgot to fill out a field, I alert them to confirm whether or not they want to go back losing changes... – Redth Nov 16 '11 at 17:06