How can I configure Launcher activity programmatically in android?-ThrowExceptions

Exception or error:

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?

How to solve:

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).


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.


The easiest way is to make MainActivity launcher activity, as usual.
Then check in MainActivity#onCreate(Bundle) via SharedPreferences if the user already logged in and, if not, start LoginActivity immediately. When user logs in, save the boolean flag indicating that user logged in in SharedPreferences and finish MainActivity.


An activity doesn’t necessarily require a UI, so you can use the launcher activity in the manifest to lauch any activity you desire.


As far as I know changing launcher programmatically is not possible, but it also doesn’t make sense.

On your LoginActivity’s onCreate check if a username and token is already saved, if it is try to login with that automatically, is succeed redirect to your MainAcivity. Depending on the way your app works you can have a variable that checks if a user is logged in or not, if he is the LoginActivity would redirect him to MainActivity without trying to log in again.

onCreate(Bundle bundle)
    /* ... */

    //Or whatever you use to login (it could also go inside a thread or an AsyncTask
    if (login())
        Intent intent = new Intent(this, MainActivity.class);

        //Start Activity

        //Finish this activity, so when user pressed back the login activity will not come forth and  the app will exit 
        //this looks like when a user has logged in once, the login screen will not be visible to him (unless you want to)

You can also configure it to save username and token only if a login is successful which means the above code can be modified like this:

if (getUsername() != null)
    /* Start Main Activity */

This won’t attempt to log in, but it knows the credential are right since it has logged in at least once with them.

If your app behaves a different way that these methods do not work, feel free to say so, I may be able to provide more info


You can jsut add Intent after OnCreate to the XML you want to show in the beginning of your APP.

public class LoginActivity extends Activity {

   protected void onCreate(Bundle savedInstanceState) {
       //add some code to detect if user is logged in
       if (user != null){
           Intent in = new Intent(LoginActivity.this, YourDesiredActivity.class);

Leave a Reply

Your email address will not be published. Required fields are marked *