我找到了一些以编程方式退出Android应用程序的代码。通过在onDestroy()中使用以下任何代码,它会完全退出应用程序吗?

System.runFinalizersOnExit(真正的) (或) android.os.Process.killProcess (android.os.Process.myPid ());

我不想在点击退出按钮后在后台运行我的应用程序。 请告知我是否可以使用这些代码中的任何一个来退出我的应用程序?如果可以,我可以使用哪个代码?在Android中退出应用程序是一个好方法吗?


当前回答

如果您确实需要关闭应用程序,请认真考虑一下:为什么不让操作系统自行决定何时何地释放资源呢?

否则,如果你非常确定,就用

finish();

作为对@dave appleton评论的回应:第一件事是阅读大问题/答案组合@gabriel的帖子:退出应用程序是不受欢迎的吗?

现在假设我们有了这个,这里的问题仍然有一个答案,如果你要做任何关于退出的事情,你需要的代码是finish()。显然你可以有不止一项活动等等,但这不是重点。让我们看一些用例

You want to let the user quit everything because of memory usage and "not running in the background? Doubtfull. Let the user stop certain activities in the background, but let the OS kill any unneeded recourses. You want a user to not go to the previous activity of your app? Well, either configure it so it doesn't, or you need an extra option. If most of the time the back=previous-activity works, wouldn't the user just press home if he/she wants to do something else? If you need some sort of reset, you can find out if/how/etc your application was quit, and if your activity gets focus again you can take action on that, showing a fresh screen instead of restarting where you were.

所以最后,当然,finish()不会杀死所有东西,但我认为它仍然是你需要的工具。如果有“杀死所有活动”的用例,我还没有找到。

其他回答

如果您确实需要关闭应用程序,请认真考虑一下:为什么不让操作系统自行决定何时何地释放资源呢?

否则,如果你非常确定,就用

finish();

作为对@dave appleton评论的回应:第一件事是阅读大问题/答案组合@gabriel的帖子:退出应用程序是不受欢迎的吗?

现在假设我们有了这个,这里的问题仍然有一个答案,如果你要做任何关于退出的事情,你需要的代码是finish()。显然你可以有不止一项活动等等,但这不是重点。让我们看一些用例

You want to let the user quit everything because of memory usage and "not running in the background? Doubtfull. Let the user stop certain activities in the background, but let the OS kill any unneeded recourses. You want a user to not go to the previous activity of your app? Well, either configure it so it doesn't, or you need an extra option. If most of the time the back=previous-activity works, wouldn't the user just press home if he/she wants to do something else? If you need some sort of reset, you can find out if/how/etc your application was quit, and if your activity gets focus again you can take action on that, showing a fresh screen instead of restarting where you were.

所以最后,当然,finish()不会杀死所有东西,但我认为它仍然是你需要的工具。如果有“杀死所有活动”的用例,我还没有找到。

我不知道这是否会引起不满,但我就是这么做的……

步骤1 -我通常有一个类,其中包含我想全局访问的方法和变量。在本例中,我将其称为“App”类。在类中为应用程序的每个活动创建一个静态Activity变量。然后创建一个名为“close”的静态方法,如果这些Activity变量不为空,它将在每个Activity变量上运行finish()方法。如果你有一个主/父活动,最后关闭它:

public class App
{
    ////////////////////////////////////////////////////////////////
    // INSTANTIATED ACTIVITY VARIABLES
    ////////////////////////////////////////////////////////////////

        public static Activity activity1;
        public static Activity activity2;
        public static Activity activity3;

    ////////////////////////////////////////////////////////////////
    // CLOSE APP METHOD
    ////////////////////////////////////////////////////////////////

        public static void close()
        {
            if (App.activity3 != null) {App.activity3.finish();}
            if (App.activity2 != null) {App.activity2.finish();}
            if (App.activity1 != null) {App.activity1.finish();}
        }
}

步骤2 -在每个活动中,重写onStart()和onDestroy()方法。在onStart()中,将App类中的静态变量设置为"this"。在onDestroy()中,将其设置为null。例如,在“Activity1”类中:

@Override
public void onStart()
{
    // RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS

        super.onStart();
        App.activity1 = this;
}

@Override
public void onDestroy()
{
    // RUN SUPER | REGISTER ACTIVITY AS NULL IN APP CLASS

        super.onDestroy();
        App.activity1 = null;
}

步骤3 -当你想关闭你的应用程序时,只需从任何地方调用app .close()。所有实例化的活动将关闭!因为你只是关闭活动,而不是杀死应用程序本身(如你的例子),Android可以自由地从那里接管,并做任何必要的清理。

再说一次,我不知道这是否会因为任何原因而引起不满。如果是这样的话,我很乐意阅读关于为什么会这样以及如何改进的评论!

如果你在Activity中,最好使用finish(),如果你在Fragment中,最好使用getActivity().finish()。

如果你想完全退出应用程序,那么使用:

getActivity().finish();
System.exit(0);

这取决于你想多快关闭你的应用程序。

关闭应用程序的安全方法是finishAffinity();

它关闭你的应用程序后,所有进程完成处理。 这可能需要一些时间。 如果你以这种方式关闭你的应用程序,并在短时间后重新启动它,你的新应用程序可能运行在同一个进程中。旧应用程序的所有未完成的进程和单例对象。

如果你想确保你的应用程序已经完全关闭,请使用System.exit(0);

这将立即关闭你的应用程序。 但这是有可能的,你破坏了你的应用程序已经打开的文件或共享首选项的编辑没有完成。所以要小心使用。

如果您将看门狗与长时间运行的任务结合使用,您可以看到不同方法的影响。

new ANRWatchDog(2000).setANRListener(new ANRWatchDog.ANRListener() {
    public void onAppNotResponding(ANRError error) {
        MainActivity.this.finishAffinity();
        System.exit(0);
    }
}).start();
for(int i = 0; i < 10; ++i){
    --i;
}

这会在2秒后杀死你的应用,而不显示ANR对话框或类似的东西。如果你删除System.exit(0),运行这段代码并在应用程序关闭后重新启动应用程序,你会遇到一些奇怪的行为,因为无休止的循环仍在运行。

public void quit() {
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
        System.exit(0);
    }