Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure Launcher activity programmatically in android?

Tags:

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?

like image 523
aruke Avatar asked Apr 08 '15 14:04

aruke


People also ask

What is launcher activity?

Displays a list of all activities which can be performed for a given intent. Launches when clicked.

What is startActivity method in android?

Starting activities or services. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.


2 Answers

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.

like image 141
CommonsWare Avatar answered Sep 28 '22 05:09

CommonsWare


Long story short, you cannot change the Activity that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.

However, there are reasonable work arounds. In your MainActivity you can check whether the user is logged in and immediately redirect them to the LoginActivity. That has the added benefit of automatically returning to the MainActivity after you have logged in.

Alternatively, you can always go first to the LoginActivity, and if the user is already logged in, send them to the MainActivity (rewrite the Intent history to remove the return to LoginActivity or set the noHistory flag in the manifest).

like image 37
Knossos Avatar answered Sep 28 '22 05:09

Knossos