Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 917
Monstieur Avatar asked Jun 03 '14 07:06

Monstieur


2 Answers

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

like image 66
wvdz Avatar answered Sep 19 '22 15:09

wvdz


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{  @Override protected 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.

like image 31
Rod_Algonquin Avatar answered Sep 19 '22 15:09

Rod_Algonquin