alpha-rion alpha-rion - 10 months ago 113
Android Question

How can I configure Launcher activity programmatically in android?

I am working on an app which is having two activities : LoginActivity and MainAcivity. When user first time opens the app he will login and his credentials (username and token) are saved in Preferences. Now, if user open 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 ?

Answer Source

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