Properly skip login activity if already logged in

Monstieur picture Monstieur · Jun 3, 2014 · Viewed 21.2k times · Source

My launcher icon currently starts the login activity. I've stored the logged in status in SharedPreferences. Is there any way to properly skip the login activity and go straight to the main activity without any UI glitches. All existing solutions involving finish() in onCreate() cause the login activity title to be briefly visible or some other brief blank screen UI glitch.

Answer

wvdz picture wvdz · Jun 3, 2014

Have a launcher acitivy with no UI that decides to open the MainActivity or the LoginActivity. You can declare no UI with:

android:theme="@android:style/Theme.NoDisplay"

Two other possible solutions:

Just do it the other way around: make your mainActivity your launcher and make it check whether the user is logged in. Then redirect to the loginActivity when this is not the case.

Another way is to work with fragments. Have a base activity that can load both the mainFragment and the loginFragment. For reference: https://developer.android.com/training/basics/fragments/index.html