Disable back button in android

Android

Android Problem Overview


How to disable back button in android while logging out the application?

Android Solutions


Solution 1 - Android

Override the onBackPressed method and do nothing if you meant to handle the back button on the device.

@Override
public void onBackPressed() {
   if (shouldAllowBack()) {
       super.onBackPressed();
   } else {
       doSomething();
   }
}

Solution 2 - Android

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;
}

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

Write above code in your Activity to prevent back button pressed

Solution 3 - Android

You can do this simple way Don't call super.onBackPressed()

Note:- Don't do this unless and until you have strong reason to do it.
@Override
public void onBackPressed() {
// super.onBackPressed();
// Not calling **super**, disables back button in current screen.
}

Solution 4 - Android

Simply override the onBackPressed() method.

@Override
public void onBackPressed() { }

Solution 5 - Android

I am using it.............

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if(keyCode==KeyEvent.KEYCODE_BACK)
		Toast.makeText(getApplicationContext(), "back press",      
     Toast.LENGTH_LONG).show();

	return false;
       // Disable back button..............
}

Solution 6 - Android

Just override the onBackPressed() method and no need to call the super class of onBackPressed method or others..

@Override
public void onBackPressed()
{
   
}

Or pass your current activity into the onBackPressed() method.

@Override
public void onBackPressed()
{
   startActivity(new Intent(this, myActivity.class));  
   finish();
}

Replace your require activity name to myActivity.

if you are using fragment then first of all call the callParentMethod() method

public void callParentMethod(){
    context.onBackPressed(); // instead of context use getActivity or something related
}

then call the empty method

@Override
public void onBackPressed()
{

}

Solution 7 - Android

If you want to make sure your android client application is logged out from some server before your Activity gets killed --> log out with a service on its own thread (that's what you're supposed to do anyway).

Disabling the back button won't solve anything for you. You'll still have the same problem when the user receives a phone call for instance. When a phone call is received, your activity has about as much chances of getting killed before it gets a reliable answer back from the network.

That's why you should let a service wait on its own thread for the answer from the network, and then make it try again if it doesn't succeed. The android service is not only much less likely to get killed before it gets an answer back, but should it really get killed before finishing the job, it can always get revived by AlarmManager to try again.

Solution 8 - Android

Disable back buttton in android

@Override
public void onBackPressed() {
    return;
}

Solution 9 - Android

You can override the onBackPressed() method in your activity and remove the call to super class.

@Override
public void onBackPressed() {
  //remove call to the super class
  //super.onBackPressed();
}

Solution 10 - Android

if you are using FragmentActivity. then do like this

first call This inside your Fragment.

public void callParentMethod(){
    getActivity().onBackPressed();
}

and then Call onBackPressed method in side your parent FragmentActivity class.

@Override
public void onBackPressed() {
  //super.onBackPressed();
  //create a dialog to ask yes no question whether or not the user wants to exit
  ...
}

Solution 11 - Android

Just using this code: If you want backpressed disable, you dont use super.OnBackPressed();

@Override
public void onBackPressed() {

}

Solution 12 - Android

If you want to disable your app while logging out, you can pop up a non-cancellable dialog.

Solution 13 - Android

You just need to override the method for back button. You can leave the method empty if you want so that nothing will happen when you press back button. Please have a look at the code below:

@Override
public void onBackPressed() 
{
   // Your Code Here. Leave empty if you want nothing to happen on back press.
}

Solution 14 - Android

Apart form these two methods from answer above.

> onBackPressed() (API Level 5, Android 2.0) > > onKeyDown() (API Level 1, Android 1.0)

You can also override the dispatchKeyEvent()(API Level 1, Android 1.0) like this,

> dispatchKeyEvent() (API Level 1, Android 1.0)

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return super.dispatchKeyEvent(event);
}

Solution 15 - Android

remove super.onBackPressed() from public void onBackPressed() work great. its tested in android 9

Solution 16 - Android

For me just overriding onBackPressed() did not work but explicit pointing which activity it should start worked well:

@Override
public void onBackPressed(){
  Intent intent = new Intent(this, ActivityYouWanToGoBack.class);
  startActivity(intent);
}

Solution 17 - Android

Try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
 if (keyCode == KeyEvent.KEYCODE_BACK) {

   return true;
 }
   return super.onKeyDown(keyCode, event);    
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGraceView Question on Stackoverflow
Solution 1 - AndroidGopinathView Answer on Stackoverflow
Solution 2 - AndroidRohit SharmaView Answer on Stackoverflow
Solution 3 - AndroidIman MarashiView Answer on Stackoverflow
Solution 4 - AndroidPankaj kumarView Answer on Stackoverflow
Solution 5 - AndroidMahmudul Haque KhanView Answer on Stackoverflow
Solution 6 - AndroidZakaria HossainView Answer on Stackoverflow
Solution 7 - AndroidStephan BranczykView Answer on Stackoverflow
Solution 8 - AndroidMahesh BokhaniView Answer on Stackoverflow
Solution 9 - AndroidAman AgarwalView Answer on Stackoverflow
Solution 10 - AndroidhashView Answer on Stackoverflow
Solution 11 - AndroidSeda T.View Answer on Stackoverflow
Solution 12 - AndroidTed HoppView Answer on Stackoverflow
Solution 13 - AndroidHammad KhanView Answer on Stackoverflow
Solution 14 - AndroidLF00View Answer on Stackoverflow
Solution 15 - AndroidGolilView Answer on Stackoverflow
Solution 16 - AndroidMsmktView Answer on Stackoverflow
Solution 17 - Androidmohamad sheikhiView Answer on Stackoverflow