androidandroid-activityandroid-applicationinfo

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?


Solution

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