Properly skip login activity if already logged in

后端 未结 5 1355
终归单人心
终归单人心 2021-01-31 03:55

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 activi

5条回答
  •  醉话见心
    2021-01-31 04:24

    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.

提交回复
热议问题