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
248 views
in Technique[技术] by (71.8m points)

How can I configure Launcher activity programmatically in android?

I am working on an app with two activities : LoginActivity and MainActivity. When the user first opens the app he will login and his credentials (username and token) are saved in Preferences.

Now, if the user opens the app again then MainActivity should start. I tried to switch between these activities in Application class and removed intent-filter for LAUNCHER_ACTIVITY from manifest, but it doesn't work.

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


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

...