登录屏幕Android:切换默认活动

时间:2015-04-14 05:59:05

标签: android

我有一个使用Login的应用程序。我想只在用户以前没有登录时才运行此活动。但这是我的默认活动。有没有方法可以跳过默认活动并直接转到下一个活动?

3 个答案:

答案 0 :(得分:5)

您应该从登录活动中拨打下一个活动(例如个人资料)。应用程序工作流程为:

1. Start Login Activity (by default);
2. Check login state;
3. If user already logged - start new Activity and close this.

怎么做?

要调用新活动,您应该使用Intent。要关闭当前,以便用户以后不能返回登录活动,您应该清除当前的应用程序backstack(即活动历史记录)。此外,您可以重置过渡动画,因此用户(如果已记录)甚至不会注意到已调用登录活动。

private void startProfileActivity() {
        Intent intent = new Intent(this, ProfileActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); //clear backstack
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); //it will looks like the transition inside the app, so user will not notice login activity, instead of default animation, which look like starting other app.
        startActivity(intent);
} 

答案 1 :(得分:3)

您可以将共享偏好用于此目的

<强> 1。登录(成功)

SharedPreferences prefs = this.getSharedPreferences(
          "com.example.app", Context.MODE_PRIVATE);
    prefs.edit().putBoolean("isLogin", true).apply();

现在切换您的活动

<强> 2。在下次打开应用时,请检查

if(prefs.getLong("isLogin", false)){
     //User already Login
}else{
     // User not logged in
}

P.S。您可以在LoginActivity的onStart()上使用此检查。

答案 2 :(得分:0)

Try This type Code:-


public class LoginActivity extends Activity implements OnClickListener {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(islogin){      //your boolean variable
        Intent intent = new Intent();
        intent.setClass(LoginActivity.this, "Your Next activity";
        startActivity(intent);
        finish();
    }
        setContentView(R.layout.activity_login);
相关问题