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