Is there a method to clear all the task in an app (Android)?

…衆ロ難τιáo~ 提交于 2020-01-30 13:19:07

问题


Quick question, just want to know if i have 2 tasks in my project. Is there any way to clear both of them in a single go. This is because when i try to clear both task to quit my app, only one is clearing and the another one is still alive which prevents me from quitting the application.

finishAffinity();
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);

Any suggestions to solve this ?


回答1:


Yes, You could use:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK

or else Use android:noHistory="true" on the splash activity in the manifest file.




回答2:


If you really want to quit both tasks, then you need to do it like this:

When you want to quit, you send a broadcast Intent with the ACTION set to something like "my.package.name.QUIT".

In all of your activities, declare and register a listener, like this:

registerReceiver(myReceiver, new IntentFilter("my.package.name.QUIT"));

When onReceive() of your BroadcastReceiver is called, you should call finish().

This will ensure that all of your activities finish, regardless of where they are running.

NOTE: I still don't understand why you have multiple tasks and I think that you need to address that problem. The solution here is really just a hack because you probably don't want multiple tasks.



来源:https://stackoverflow.com/questions/59767509/is-there-a-method-to-clear-all-the-task-in-an-app-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!