How to launch an Activity in different task stack?

Cheok Yan Cheng :

I have seen some app able to launch Activity in different task stack.

The Activity launched from app icon and the Activity launched from home widget, will both re-inside different task stack.

Please see the screenshot below.

enter image description here


I try to achieve the same behavior. In my home widget code, whenever I want to launch Activity, I will use the following flag.

Intent i = new Intent(context, HomeWidgetLauncherFragmentActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(i);

However, I fail to achieve the same behavior as above screenshot. There are always only 1 task stack for my app.

Is there anything I had missed out?

Pavneet_Singh :

You are using the same taskAffinity, as mentioned here

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.

so with FLAG_ACTIVITY_NEW_TASK you need to provide an affinity to create a different task stack using

    <activity
        android:taskAffinity="com.example.widget"
        android:name=".WidgetActivity"
        android:label="@string/title_activity_widget"
        android:theme="@style/AppTheme.NoActionBar"></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.

Default affinity string value is the package name attribute in manifest.

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174507&siteId=1