Block Back Button in android
I want to block hardware back button in android ,in order to prevent from going back 开发者_Python百科to other activity.. Thanks in advance...
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
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.
精彩评论