Search code examples
androidandroid-activityandroid-lifecycleandroid-launcher

Properly skip login activity if already logged in


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.


Solution

  • 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