Reputation: 101
`The LauncherActivity is the first screen in Homescreen. I am trying to understand if singleTop or singleTask would be the right launchmode. The idea is to avoid creating multiple activity instances in all transitions to Homescreen.
The activity configuration in Manifest is -
<activity
android:name=".ui.LauncherActivity"
android:clearTaskOnLaunch="true"
android:exported="true"
android:stateNotNeeded="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER_APP" />
</intent-filter>
</activity>
<activity
android:name=".ui.AppDrawerActivity"
android:exported="true"
android:permission="android.permission.QUERY_ALL_PACKAGES">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The activity can be triggered only from a non-acitivty application, which belongs to a different package from LauncherActivity. It is launched like below-
private void startHomeActivity() {
Intent intent = new Intent();
intent.setAction(Constants.ACTION_MAIN);
intent.addCategory(Constants.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(Constants.HOME_PACKAGE_NAME,
Constants.HOME_ACTIVITY_CLASS_NAME));
intent.addFlags(Constants.SETTINGS_HOME_AND_ALL_APPS_FLAG);
mContext.startActivityAsUser(intent, UserHandle.CURRENT);
}
Observation: Currently singleTop is set as launchmode for the LauncherActivity. Please note that, AppDrawerActivity is another acitivty, which belong to Homescreen application package.
The transition is done as below-
at this point the activity dump shows the stack as below. Here, we can see that in task#1200003, AppDrawerActivity is on the top of the stack.
Task display areas in top down Z order:
TaskDisplayArea DefaultTaskDisplayArea
mPreferredTopFocusableRootTask=Task{82141e #1200004 type=standard A=1210072:com.apps.hvac U=12 visible=true visibleRequested=true mode=fullscreen translucent=false sz=1}
mLastFocusedRootTask=Task{82141e #1200004 type=standard A=1210072:com.apps.hvac U=12 visible=true visibleRequested=true mode=fullscreen translucent=false sz=1}
Application tokens in top down Z order:
* Task{82141e #1200004 type=standard A=1210072:com.apps.hvac U=12 visible=true visibleRequested=true mode=fullscreen translucent=false sz=1}
bounds=[0,0][1280,720]
* ActivityRecord{63ba8ca u12 com.apps.hvac/.ui.HvacLandingActivity t1200004}
* Task{57a2911 #1 type=home ?? U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=2}
bounds=[0,0][1280,720]
* Task{4bd5acd #1200003 type=home A=1201000:com.apps.homescreen U=12 rootTaskId=1 visible=false visibleRequested=false mode=fullscreen translucent=true sz=2}
bounds=[0,0][1280,720]
* ActivityRecord{34c92b u12 com.apps.homescreen/.ui.AppDrawerActivity t1200003}
* ActivityRecord{d1d210e u12 com.apps.homescreen/.ui.LauncherActivity t1200003}
* Task{4089178 #2 type=home A=10132:com.android.car.provision U=0 rootTaskId=1 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
bounds=[0,0][1280,720]
* ActivityRecord{6be6283 u0 com.android.car.provision/.DefaultActivity t2}`
The we click on Home button, which is handled in a different non-acitivty application. Now the stack looks as below.
Task display areas in top down Z order:
TaskDisplayArea DefaultTaskDisplayArea
mPreferredTopFocusableRootTask=Task{57a2911 #1 type=home ?? U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=2}
mLastFocusedRootTask=Task{57a2911 #1 type=home ?? U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=2}
Application tokens in top down Z order:
* Task{57a2911 #1 type=home ?? U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=2}
bounds=[0,0][1280,720]
* Task{4bd5acd #1200003 type=home A=1201000:com.apps.homescreen U=12 rootTaskId=1 visible=true visibleRequested=true mode=fullscreen translucent=false sz=1}
bounds=[0,0][1280,720]
* ActivityRecord{d1d210e u12 com.apps.homescreen/.ui.LauncherActivity t1200003}
* Task{4089178 #2 type=home A=10132:com.android.car.provision U=0 rootTaskId=1 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
bounds=[0,0][1280,720]
* ActivityRecord{6be6283 u0 com.android.car.provision/.DefaultActivity t2}
* Task{82141e #1200004 type=standard A=1210072:com.apps.hvac U=12 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
bounds=[0,0][1280,720]
* ActivityRecord{63ba8ca u12 com.apps.hvac/.ui.HvacLandingActivity t1200004}
public static final int SETTINGS_HOME_AND_ALL_APPS_FLAG = 0x14000000;
private void launchAppDrawerScreen() {
Intent intent = new Intent();
intent.setAction(Constants.ACTION_MAIN);
intent.addCategory(Constants.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(Constants.HOME_PACKAGE_NAME,
Constants.APP_DRAWER_ACTIVITY_CLASS_NAME));
intent.addFlags(Constants.SETTINGS_HOME_AND_ALL_APPS_FLAG); // launchFlags
mContext.startActivityAsUser(intent, UserHandle.CURRENT);
}
Manifest declaration:
<activity
android:name=".ui.AppDrawerActivity"
android:exported="true"
android:permission="android.permission.QUERY_ALL_PACKAGES">
<intent-filter>
<action android:name="com.bosch.apps.homescreen.ACTION_APP_GRID" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
**Could anyone please help to understand these points?
As per the document, when a singleTop acitivty is started, if it is not at the top of the task, it should be recreated. In this case, AppDrawerActivity was at the top of task. But still why the previous instance of LauncherActivity was reused instead of recreating it?
In this case which launchmode would be ideal in between singleTop and singleTask?**
`
Upvotes: 1
Views: 100
Reputation: 95568
Well, finally! This explains the behaviour!
public static final int SETTINGS_HOME_AND_ALL_APPS_FLAG = 0x14000000;
private void startHomeActivity() {
Intent intent = new Intent();
intent.setAction(Constants.ACTION_MAIN);
intent.addCategory(Constants.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(Constants.HOME_PACKAGE_NAME,
Constants.HOME_ACTIVITY_CLASS_NAME));
intent.addFlags(Constants.SETTINGS_HOME_AND_ALL_APPS_FLAG);
mContext.startActivityAsUser(intent, UserHandle.CURRENT);
}
You are starting LauncherActivity
with the following flags in the Intent
:
FLAG_ACTIVITY_NEW_TASK = 0x10000000;
FLAG_ACTIVITY_CLEAR_TOP = 0x04000000;
Because FLAG_ACTIVITY_CLEAR_TOP
is set, this will remove any Activity
that is in the task on top of the existing LauncherActivity
. This is why AppDrawerActivity
is removed from the task when you press the HOME button.
Since the manifest declaration for LauncherActivity
has launchMode="singleTop"
, the combination of CLEAR_TOP
and SINGLE_TOP
tells Android to remove any activities on top of the existing LauncherActivity
and to reuse the existing instance of LauncherActivity
.
This all works as documented and as designed. If this isn't what you want, you will need to explain what you want to happen and I can suggest a way to achieve that.
Upvotes: 0