Android if the user is not logged in, skip to the login page where should this logic be implemented in activity?

android if the user is not logged in, skip to the login page where should this logic be implemented in activity?

Feb.28,2021

there are two ways to deal with it:

  1. @ ShawnXiaFei
  2. write to BaseActivity
public class BaseActivity extends AppCompatActivity {
    public boolean acquireAuthorization() {
        if (!isAuthorized()) {
            if (!onAcquireAuthorization()) {
                finish();            
                return false;
            }
        }
        return true;
    }
    
    public boolean isAuthorized() {
        return true;
    }

    public boolean onAcquireAuthorization() {
        return false;
    }
}
public class AppActivity extends BaseActivity {
    @Override
    public boolean isAuthorized() {
        // TODO
        return super.isAuthorized();
    }

    @Override
    public boolean onAcquireAuthorization() {
        // TODO
        return super.onAcquireAuthorization();
    }
}
public class MainActivity extends AppActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (acquireAuthorization()) {
            // TODO ...
        }
    }
}

1, add the startup page, make a judgment on the startup page, log in to the home page, and jump to the login page if you don't log in.
2. When judging the onCreate of the home page, jump directly to the login page without logging in, and finish the home page at the same time.

Menu