Android 如何杀死应用程序及其所有活动?

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

How to kill an application with all its activities?

androidkill

提问by Nils

Possible Duplicate:
Quitting an application - is that frowned upon?

可能的重复:
退出应用程序 - 这是否令人不悦?

I want to offer the user an option to exit the application as I need to delete some sensitive data, which is stored in the SharedPreferences as long as the application needs it.

我想为用户提供退出应用程序的选项,因为我需要删除一些敏感数据,只要应用程序需要这些数据就存储在 SharedPreferences 中。

As soon as the user wants to exit, the password in the SharedPreferences should be wiped and of course all activities of the application should be closed (it makes no sense to run them without the known password - they would crash).

一旦用户想要退出,就应该清除 SharedPreferences 中的密码,当然应该关闭应用程序的所有活动(在没有已知密码的情况下运行它们是没有意义的 - 它们会崩溃)。

How can I do that?

我怎样才能做到这一点?

System.exit(0)and finish()only exit the current activity - useless. I know there is a taskmanager app. How is that one doing it? It's able to kill the whole application...

System.exit(0)并且finish()只退出当前活动 - 没用。我知道有一个任务管理器应用程序。那个人是怎么做的?它能够杀死整个应用程序......

回答by Thirumalvalavan

When you use the finish() method, it does not close the process completely , it is STILL working in background.

当您使用 finish() 方法时,它不会完全关闭进程,它仍在后台工作。

Please use this code in Main Activity (Please don't use in every activities or sub Activities):

请在主活动中使用此代码(请不要在每个活动或子活动中使用):

@Override
public void onBackPressed() {

    android.os.Process.killProcess(android.os.Process.myPid());
    // This above line close correctly
}

回答by mtmurdock

You are correct: calling finish()will only exit the current activity, not the entire application. however, there is a workaround for this:

你是对的:调用finish()只会退出当前活动,而不是整个应用程序。但是,有一个解决方法:

Every time you start an Activity, start it using startActivityForResult(...). When you want to close the entire app, you can do something like this:

每次启动 Activity 时,请使用startActivityForResult(...). 当您想关闭整个应用程序时,您可以执行以下操作:

setResult(RESULT_CLOSE_ALL);
finish();

Then define every activity's onActivityResult(...)callback so when an activity returns with the RESULT_CLOSE_ALLvalue, it also calls finish():

然后定义每个活动的onActivityResult(...)回调,以便当活动返回RESULT_CLOSE_ALL值时,它还调用finish()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode)
    {
    case RESULT_CLOSE_ALL:
        setResult(RESULT_CLOSE_ALL);
        finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

This will cause a cascade effect closing all activities.

这将导致关闭所有活动的级联效应。

Also, I support CommonsWare in his suggestion: store the password in a variable so that it will be destroyed when the application is closed.

另外,我在他的建议中支持 CommonsWare:将密码存储在变量中,以便在应用程序关闭时将其销毁。

回答by Eric Leschinski

When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".

当用户希望退出所有打开的 Activity 时,他们应该按下一个按钮,该按钮加载应用启动时运行的第一个 Activity,在我的例子中是“LoginActivity”。

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.

上面的代码清除了除 LoginActivity 之外的所有活动。LoginActivity 是用户运行程序时出现的第一个活动。然后将此代码放在 LoginActivity 的 onCreate 中,以在传递“退出”消息时发出自毁信号。

    if (getIntent().getBooleanExtra("EXIT", false)) {
         finish();
    }

The answer you get to this question from the Android platform is: "Don't make an exit button. Finish activities the user no longer wants, and the Activity manager will clean them up as it sees fit."

你从 Android 平台得到的这个问题的答案是:“不要制作退出按钮。完成用户不再想要的活动,活动管理器会在它认为合适的时候清理它们。”

回答by CommonsWare

which is stored in the SharesPreferences as long as the application needs it.

只要应用程序需要它,它就会存储在 SharesPreferences 中。

Why?

为什么?

As soon as the user wants to exit, the password in the SharedPreferences should be wiped and of course all activities of the application should be closed (it makes no sense to run them without the known password - they would crash).

一旦用户想要退出,就应该清除 SharedPreferences 中的密码,当然应该关闭应用程序的所有活动(在没有已知密码的情况下运行它们是没有意义的 - 它们会崩溃)。

Even better: don't put the password in SharedPreferences. Hold onto it in a static data member. The data will naturally go away when all activities in the app are exited (e.g., BACK button) or otherwise destroyed (e.g., kicked out of RAM to make room for other activities sometime after the user pressed HOME).

更好的是:不要把密码放在SharedPreferences. 在静态数据成员中保留它。当应用程序中的所有活动都退出(例如,返回按钮)或以其他方式被破坏(例如,在用户按下 HOME 后的某个时间,踢出 RAM 为其他活动腾出空间)时,数据自然会消失。

If you want some sort of proactive "flush password", just set the static data member to null, and have your activities check that member and take appropriate action when it is null.

如果您想要某种主动的“刷新密码”,只需将静态数据成员设置为null,并让您的活动检查该成员并在该成员为 时采取适当的措施null

回答by Jorgesys

Using onBackPressed()method:

使用onBackPressed()方法:

@Override
public void onBackPressed() {    
    android.os.Process.killProcess(android.os.Process.myPid());
}

or use the finish()method, I have something like

或使用该finish()方法,我有类似的东西

//Password Error, I call function
    Quit();             


    protected void Quit() {
        super.finish();
    }

With super.finish() you close the super class's activity.

使用 super.finish() 关闭超类的活动。

回答by tlayton

My understanding of the Android application framework is that this is specifically not permitted. An application is closed automatically when it contains no more current activities. Trying to create a "kill" button is apparently contrary to the intended design of the application system.

我对 Android 应用程序框架的理解是,这是明确不允许的。当应用程序不再包含更多当前活动时,它会自动关闭。试图创建一个“杀死”按钮显然与应用程序系统的预期设计背道而驰。

To get the sort of effect you want, you could initiate your various activities with startActivityForResult(), and have the exit button send back a result which tells the parent activity to finish(). That activity could then send the same result as part of its onDestroy(), which would cascade back to the main activity and result in no running activities, which should cause the app to close.

为了获得您想要的那种效果,您可以使用 startActivityForResult() 启动您的各种活动,并让退出按钮发回一个结果,该结果告诉父活动完成()。然后,该活动可以将相同的结果作为其 onDestroy() 的一部分发送,这将级联回主活动并导致没有正在运行的活动,这应该会导致应用程序关闭。