Is there a method to clear all the task in an app

2020-05-10 05:58发布

问题:

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.