Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block Back Button in android

Tags:

android

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

like image 391
Kakey Avatar asked Oct 21 '10 14:10

Kakey


People also ask

How do I disable Home button on Android?

Navigate to Android > Restrictions > Basic and click on Configure. Under Allow Device Functionality, you'll have the options to disable Home/Power button. Home Button-Uncheck this option to restrict users from using the Home Button. Power Off-Uncheck this option to restrict users from turning their devices off.

How do I disable back press in fragment?

Here is the new way you can manage your onBackPressed() in fragment with the new call back of activity: // Disable onBack click requireActivity(). onBackPressedDispatcher. addCallback(this) { // With blank your fragment BackPressed will be disabled. }


1 Answers

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

like image 129
100rabh Avatar answered Oct 10 '22 20:10

100rabh