How can I configure Launcher activity programmatically in android?

Is there any way of switching between Launcher Activities programmatically on basis of saved preferences ?

You can try this:

Step #1: Have LoginActivity have the LAUNCHER <intent-filter> as normal, and have MainActivity have no <intent-filter>.

Step #2: Have an <activity-alias> element in the manifest pointing to MainActivity that has the LAUNCHER <intent-filter>.

Step #3: Put android:enabled="false" on the <activity-alias>, so it is disabled by default, so when the app is first installed, the only launcher icon is for LoginActivity.

Step #4: When the user logs in, and you want to change so MainActivity is the launcher activity, use PackageManager and setComponentEnabledSetting() to make the <activity-alias> enabled and to disable the LoginActivity.

Not all home screens will detect this change on the fly, and for those, the device would need to reboot in all likelihood to pick up the change. For this reason, it would be better to stick with a single launcher activity. If you want, that launcher activity could have Theme.NoDisplay and simply route to the correct “real” activity in onCreate(), per Fahim’s answer.

Leave a Comment