Disable back button in android

2018-12-31 10:14发布

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

14条回答
只靠听说
2楼-- · 2018-12-31 10:55

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

@Override
public void onBackPressed() {
   if (!shouldAllowBack()) {
       doSomething();
   } else {
       super.onBackPressed();
   }
}
查看更多
余生无你
3楼-- · 2018-12-31 10:55

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..............
}
查看更多
美炸的是我
4楼-- · 2018-12-31 10:56

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

查看更多
宁负流年不负卿
5楼-- · 2018-12-31 11:02

Simply override the onBackPressed() method.

@Override
public void onBackPressed() { }
查看更多
一个人的天荒地老
6楼-- · 2018-12-31 11:05

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();
}
查看更多
只靠听说
7楼-- · 2018-12-31 11:07

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
  ...
}
查看更多
登录 后发表回答