退出Android应用Android

2023-09-12 04:37:52 作者:有几分喜欢就有几分甘愿

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

和使用这种方法

moveTaskToBack(true);

我开发一个应用程序,并在我需要退出它的最后一项活动,我已经使用了退出应用程序的问题是,它存在的唯一的活动和应用程序在后台运行上面的方法(见于任务经理)。每当我重新加载应用程序就开始在我离开它(最后一项活动)。

I'm developing an application and at the last activity I need to exit it, I have used the above methods which exits the application the problem is it exists the ONLY the activity and the application runs in background (seen in the task manager). Whenever I load the application again it starts with where I exit it (the last activity).

有没有code彻底退出,并同时删除表格的背景(任务管理器)。

is there any code to exit it completely and also remove form background(task manager).

在此先感谢。

推荐答案

要退出应用程序,返回到您的根系活力这种方式(即第一个启动器启动活动):

To exit your application, return to your root activity (the activity that is the first one the launcher starts) this way:

Intent intent = new Intent(this, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addExtra("exit", "true");
startActivity(intent);

这将清除任务堆栈下到根系活力,然后用这个意图再次启动根系活力。

This will clear the task stack down to the root activity and then start the root activity again with this intent.

在您的根系活力,在的onCreate()您需要确定阉基础上的意图演员退出,像这样的:

In your root activity, in onCreate() you need to determine wether to exit based on the extras in the intent, like this:

Intent intent = getIntent();
if (intent.hasExtra("exit")) {
    // User wants to exit
    finish();
}

您也谈一下从任务管理器中删除您的应用程序。如果指的是最近的应用程序的列表这不是​​运行的应用程序的列表,它是用户最近使用的应用程序的列表。仅仅因为你的应用程序显示在那里并不意味着它正在运行。

You also say something about removing your app from the task manager. If you are referring to the "list of recent apps" this isn't a list of the "running applications", it is a list of the applications that the user recently used. Just because your app shows up there does not mean that it is running.

如果你真的想你的应用程序不会显示在最近的应用程序列表,只是将它添加到您的根系活力清单条目:

If you really want your app not to show up in the "list of recent apps", just add this to the manifest entry for your root activity:

android:excludeFromRecents="true"