如何禁用返回按钮在安卓而注销应用程序?


当前回答

对我来说,只是覆盖onBackPressed()不起作用,但明确指出哪个活动应该开始工作:

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

其他回答

您可以在活动中重写onBackPressed()方法,并删除对超类的调用。

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

你可以用简单的方法做到这一点

注意:除非你有足够的理由,否则不要这么做。

@Override
public void onBackPressed() {
// super.onBackPressed();
// Not calling **super**, disables back button in current screen.
}

从public void中删除super.onBackPressed() 它在安卓9上进行了测试

如果你想在注销时禁用应用程序,你可以弹出一个不可取消的对话框。

你只需要重写后退按钮的方法。如果您愿意,可以让该方法为空,这样当您按下返回按钮时就不会发生任何事情。请看下面的代码:

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