在 Android 中禁用“返回”按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20623659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
disable "BACK" button in Android?
提问by chicharp
Is their a way of disable a BACK button on my mobile? because while i'll transfer it to my mobile the BACK button is activated so that it can back the previous page. Please help me to disable BACK button while my app is running.
他们是在我的手机上禁用后退按钮的一种方式吗?因为当我将它传输到我的手机时,返回按钮被激活,以便它可以返回上一页。请帮助我在我的应用程序运行时禁用后退按钮。
回答by Glenn
Override the onBackPressed()
of your activity
覆盖onBackPressed()
您的活动
@SuppressLint("MissingSuperCall")
@Override
public void onBackPressed()
{
// super.onBackPressed(); // Comment this super call to avoid calling finish() or fragmentmanager's backstack pop operation.
}
You may suppress lint's error by adding @SuppressLint("MissingSuperCall")
as per Matthew Read pointed out.
您可以通过@SuppressLint("MissingSuperCall")
按照 Matthew Read 指出的添加来抑制 lint 的错误。
回答by Phil
This way works for all versions of Android, and will work with libraries that may override the default functionality in their Activities
(such as cocos2d-x):
这种方式适用于所有版本的Android,并且适用于可能覆盖其默认功能的库Activities
(例如cocos2d-x):
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return (keyCode == KeyEvent.KEYCODE_BACK ? true : super.onKeyDown(keyCode, event));
}
回答by Hassaan Rabbani
This is very simple.. put this code in every activity of your android code if you wish to disable the back button for complete aplication
这很简单.. 如果您希望禁用后退按钮以完成应用程序,请将此代码放在您的 android 代码的每个活动中
@Override
public void onBackPressed()
{
//thats it
}
and you are done
你就完成了