暂停时杀死android应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1024803/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:36:42  来源:igfitidea点击:

Killing android application on pause

androidandroid-1.5-cupcake

提问by Tom

I have an application which I would like to be fully disabled/closed when it is paused (IE. When the user presses the Home, End (call) and Back button I would like the application to be closed, instead of being saved in the history stack).

我有一个应用程序,我希望在暂停时完全禁用/关闭它(即。当用户按下主页、结束(呼叫)和返回按钮时,我希望应用程序被关闭,而不是保存在历史堆栈)。

How do I do this....?

我该怎么做呢....?

Thanks.

谢谢。

回答by CommonsWare

Implement onPause()in your activity and call finish()on your activity. Bear in mind, though, that this will occur on every pause, including dialogs, incoming calls, users activating a Notification. You might want to consider doing finish()in onStop(), which would at least solve the dialog problem.

onPause()在您的活动中实施并呼吁finish()您的活动。但请记住,这将在每次暂停时发生,包括对话、来电、用户激活Notification. 您可能要考虑执行finish()in onStop(),这至少可以解决对话问题。

Also, bear in mind that users will may get confused when using your app, thinking it has crashed since it is gone when they try to get back to it.

此外,请记住,用户在使用您的应用程序时可能会感到困惑,认为它已经崩溃了,因为当他们尝试重新使用它时它已经消失了。

回答by Qlimax

you can easily do that by setting true the "noHistory" attribute in to your activity element, in the manifest

您可以通过在清单中将“noHistory”属性设置为 true 来轻松地做到这一点

http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

回答by blorgggg

You know how you have an OnCreate() method in your activity which performs actions when you start. You need to add something like:

您知道如何在您的活动中使用 OnCreate() 方法,该方法在您开始时执行操作。您需要添加如下内容:

@Override

protected void onPause(){
finish();

        super.onPause();

}

in your activity to add actions before it starts

在您的活动开始之前添加操作

in this case the

在这种情况下

finish(); 

command is what you want to execute before your activity pauses.

command 是您要在活动暂停之前执行的内容。