简体   繁体   中英

Launcher Activity not launched from recent Apps

Suppose there are two Activities :
1. Activity A (Launcher Activity)
2. Activity B


When Launching Activity B from Activity A , I finish the Activity A .

startActivity(new Intent(A.this, B.class));
finish();

and If exit button clicked from Activity B , the Activity B get finished.

ExitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

So, suppose Activity B get launched from Activity A , and I get exit from Activity B (or exit from Application). There are 2 option, from where the application get launched :
1. From launcher icon
2. From Recent Apps

Now, If the application launched from launcher icon , then Activity A get launched. But If the application launched from Recent Apps , then Activity B get launched.


I think, Activity B remains in stack, even if the I call finish() for it.
My Question is : How can I clear the stack, when application get exit ? Tested in Android 2.3.6 version

You can set the android:clearTaskOnLaunch="true" attribute for you MainActivity in the AndroidManifest.xml file. I think this is the most convenient way to meet your demand.

I just tested and found this only works when you exit the app and launch the app from the app drawer(NOT long press on HOME and select the app).

If you want to always bring the root activity to the front, no matter when you re-launch the app or from the recent screen. You can declare "android:launchMode="singleTask" for the root activity, here, the MainActivity.

Beside my above mentioned solution of using fragments , you can also try writing android:excludeFromRecents for your activity in AndroidManifest

android:excludeFromRecents

Whether or not the task initiated by this activity should be excluded from the list of recently used applications, the overview screen. That is, when this activity is the root activity of a new task, this attribute determines whether the task should not appear in the list of recent apps. Set "true" if the task should be excluded from the list; set "false" if it should be included. The default value is "false"

Edit

If you follow the above it may be that your MainActivity also disappears from Recent Activity as by default all the activities of an application have the same affinity.To specify a different task use android:taskAffinity for which the docs says

android:taskAffinity

The task that the activity has an affinity for. Activities with the same affinity conceptually belong to the same task (to the same "application" from the user's perspective). The affinity of a task is determined by the affinity of its root activity.

The affinity determines two things — the task that the activity is re-parented to (see the allowTaskReparenting attribute) and the task that will house the activity when it is launched with the FLAG_ACTIVITY_NEW_TASK flag.

By default, all activities in an application have the same affinity. You can set this attribute to group them differently, and even place activities defined in different applications within the same task. To specify that the activity does not have an affinity for any task, set it to an empty string.

If this attribute is not set, the activity inherits the affinity set for the application (see the element's taskAffinity attribute). The name of the default affinity for an application is the package name set by the element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM