Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

android - Close application and remove from recent apps/

I know this question is common and asked many times on Stack Overflow, but after visiting nearly the four pages of search engine results and nearly the 20 question of Stack Overflow regarding this issue, I found that none of them is resolved or answered correctly.

What I want:

I want to show my app in recent apps list when it is running but when I close app then my process should be killed and application should be removed from recent apps list.

Some answers I found:

use System.exit(0); //doesn't clear app from recents
OR
use android.os.Process.killProcess(android.os.Process.myPid()); //doesn't clear app from recents
OR
use finish() or this.finish() or Activity.finish();// doesn't clear app from recents

And one common suggestion I see in every answer that add below code in manifest:

android:excludeFromRecents //I think this is wrong approach.

because after adding this when user presses home button while my app is running then user cannot see my app in recent Apps List.

And many other suggestions on this but none of them do both tasks close application and clear application from recent apps list. Also if you go in Settings>Apps>yourApp>your Application see that still it asks for "force stop" means application is running!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...