Close application and remove from recent apps/
Solution 1:
I based my solution on guest's above, as well as gilsaints88's comments below (for Android L compatibility):
Add this activity to your AndroidManifest.xml file:
<activity
android:name="com.example.ExitActivity"
android:theme="@android:style/Theme.NoDisplay"
android:autoRemoveFromRecents="true"/>
Then create a class ExitActivity.java:
public class ExitActivity extends Activity
{
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT >= 21)
{
finishAndRemoveTask();
}
else
{
finish();
}
}
public static void exitApplication(Context context)
{
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
Then whenever you want to force close your application and remove it from the recent's list, call:
ExitActivity.exitApplication(context);
This solution works for me instead of declaring android:excludeFromRecents="true" because I want the user to be able to see the app in the recents list UNTIL the point where my app triggers closing the app programmatically.
Solution 2:
Before I present my attempt at this, I want to make it clear that what follows won't address the availability of "force stop" in the application info. Android allows you to force stop an application even if it does not have any processes running. Force stop puts the package into a specific stopped state, where it can't receive broadcast events.
Now that that's out of the way, on with my cheesy idea for this. The gist of it is to replace the task that you want to exit with one that the system will exclude from the recent apps list.
- Define an activity (I'll call it
Exiter
) that just finishes itself immediately.- Call
finish()
inonCreate(Bundle)
. - From the question, it sounds like the OP will also want to call
System.exit(int)
too. But for other people reading this answer, you usually don't have to kill the process. - Use
android:theme="@android:style/Theme.NoDisplay"
to prevent this from causing a bunch of redundant transitions.
- Call
- Set
android:taskAffinity
so thatExiter
will run in the task that you want to exit. Or don't: by default, unspecifiedtaskAffinity
makes it so that all activities share a common anonymoustaskAffinity
. - When you want to exit, use
startActivity(Intent)
to startExiter
with the following flags:-
Intent.FLAG_ACTIVITY_NEW_TASK
- Even though we want to start the activity in the same task, the other flags require this flag. Fortunately, thetaskAffinity
will prevent the system from actually starting a new task. -
Intent.FLAG_ACTIVITY_CLEAR_TASK
- This finishes all the activities on the task. You won't have to callfinish()
everywhere, just inExiter
. -
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
- The task won't be in the recent apps list. Since it's the same task as the one that's exiting, this flag removes the task from the list.
-
https://gist.github.com/anonymous/9884978