Disable back button in android

后端 未结 17 1346
名媛妹妹
名媛妹妹 2020-11-22 05:10

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

相关标签:
17条回答
  • 2020-11-22 05:44

    Disable back buttton in android

    @Override
    public void onBackPressed() {
        return;
    }
    
    0 讨论(0)
  • 2020-11-22 05:44

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

    0 讨论(0)
  • 2020-11-22 05:45

    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();
       }
    }
    
    0 讨论(0)
  • 2020-11-22 05:45

    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.

    0 讨论(0)
  • 2020-11-22 05:48

    Try this:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
    
       return true;
     }
       return super.onKeyDown(keyCode, event);    
    }
    
    0 讨论(0)
  • 2020-11-22 05:50

    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.
    }
    
    0 讨论(0)
提交回复
热议问题