简体   繁体   English

完全退出安卓应用

[英]Exit completely from android app

I want to exit entire app by clicking exit button.我想通过单击退出按钮退出整个应用程序。 Actually I am using exit button instead of log-out.实际上我使用退出按钮而不是注销。 So whenever user clicks on it, it should finish complete app but it destroys only last running activity.所以每当用户点击它时,它应该完成完整的应用程序,但它只会破坏最后运行的活动。 What should i do now?我现在该怎么办? I am using this method in my code, any help will be appreciated, thanks in advance.我在我的代码中使用这种方法,任何帮助将不胜感激,提前致谢。

         public void exit(View v){
                finish();
                System.exit(0);
            }

I have also used following code but it kills only last running activity and not the complete application.我也使用了以下代码,但它只杀死最后运行的活动,而不是完整的应用程序。

             public void exit(View v){
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }

There is an alternative for otherwise killing your app completely.有一种替代方法可以完全杀死您的应用程序。 Minimize it and the Android system will kill it automatically when necessary.将它最小化,Android 系统会在必要时自动杀死它。 To do so,为此,

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Furthermore, this SO question may help: How to close android app completely此外,这个问题可能会有所帮助: 如何完全关闭 android 应用程序

If you really, really have to, do this:如果你真的,真的必须这样做:

public void exit(View v){
    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
}

Don't ever put an Exit button on an Android app.永远不要在 Android 应用程序上放置退出按钮。

Read here:在这里阅读:

Closing Application with Exit button 使用退出按钮关闭应用程序

If minimum sdk version is 21 and up.如果最低 sdk 版本为 21 及更高版本。

finishAndRemoveTask()

So the app will be closed completely even from the task list.因此,即使从任务列表中,该应用程序也将完全关闭。

I think the solution suggested from Hans Kratz is not the best one, because it may cause in problems since ressources will not be finalized first.我认为 Hans Kratz 建议的解决方案不是最好的解决方案,因为它可能会导致问题,因为资源不会首先最终确定。 Instead, you should kill your app safely by using the following code:相反,您应该使用以下代码安全地终止您的应用程序:

public void exit(View v){
     System.runFinalizersOnExit(true);
     System.exit(0);
}

But really think about it twice, because it is not the way Android apps usually work.但真的要三思而后行,因为这不是 Android 应用程序通常的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM