Properly skip login activity if already logged in Properly skip login activity if already logged in android android

Properly skip login activity if already logged in


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


You can create a Base Activity that will check if the user's username and password is already in the SharedPreferences and starts the activity if it exist hence not.

example:

public class BeanStalkBaseActivity extends SherlockActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    if(SavedPreference.getUserName(this).length() == 0)    {        Intent intent = new Intent(this,LoginActivity.class);        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        finish();        startActivity(intent);    }else    {        Intent intent = new Intent(this,MainActivity.class);        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        finish();        startActivity(intent);    }}

}

BeanStalkBaseActivity should be your Launcher as it only serve as a checker.


You can also check for login status during your splash screen activity if you have one. Splash screens are great for letting users know the app hasn't stalled when it's loading and can also be used to redirect the app to the appropriate screen.

I followed this great guide my first time making one: https://www.bignerdranch.com/blog/splash-screens-the-right-way/