在Android中正确检查用户是否已登录。

huangapple go评论69阅读模式
英文:

Correctly check if user is logged in. Android

问题

在我的MainActivity.java中,我使用Retrofit从服务器进行身份验证,并且使用令牌API进行身份验证:

apiInterface = ApiClient.getClient().create(ApiInterface.class);

User user = SharedPreferencesHelper.getUser(MainActivity.this);

if (user.getToken() == null) {
    Intent login = new Intent(MainActivity.this, LoginActivity.class);
    startActivity(login);
} else {
    setContentView(R.layout.activity_main);
    buildMain();
}

在我的LoginActivity.java中,我有一个问题,用户登录后显示一个空白页面,而不是预期的activity_main.xml,但当我关闭应用并重新打开时,它会像预期的那样直接跳转到activity_main.xml。是否有原因导致登录后没有像预期的那样跳转到activity_main

英文:

I have my login setup like this, I use retrofit to authenticate from the server and I'm using a token api authentication:

In the MainActivity.java method onCreate

apiInterface = ApiClient.getClient().create(ApiInterface.class);

User user = SharedPreferencesHelper.getUser(MainActivity.this);

if (user.getToken() == null) {
    Intent login = new Intent(MainActivity.this, LoginActivity.class);
    startActivity(login);
} else {
    setContentView(R.layout.activity_main);
    buildMain();
}

In the LoginActivity.java method on create (I'll summarize, the code is quite long)

loginbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //get login ingo

        Login login = new Login(scardI, passwordI, device_name);

        Call<User> call = apiInterface.LoginUser(login);

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    User user = response.body();
                    //save user info into SharedPreferences
                    SharedPreferencesHelper.setUser(LoginActivity.this, user);
                    
                    //check if user saved correctly by getting the user token
                    if (SharedPreferencesHelper.getUserToken(LoginActivity.this) != null) {
                        finish();
                    }
                } else {
                    //show error message
                }
            }
            @Override
            public void onFailure(Call<User> call, Throwable t) {
                //show error message
            }
        });
    }
});

I'm having an issue, after the user logs in a blank page is displayed instead of the activity_main.xml however when I close the app and reopen it it takes me straight to the activity_main.xml as expected. Is there a reason that after the login it doesn't take me to the activity_main like expected.

答案1

得分: 1

如果响应成功缺少跳转至 MainActivity.java 的意图由于调用了 finish() 方法所以显示为空白屏幕

if (response.isSuccessful()) {
    User user = response.body();
    SharedPreferencesHelper.setUser(LoginActivity.this, user);
    if (SharedPreferencesHelper.getUserToken(LoginActivity.this) != null) {
        Intent i = new Intent(this, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(i);
    }
}
英文:

An intent to MainActivity.java is missing on success. A blank screen is displayed because finish() is called.

 if (response.isSuccessful()) {
     User user = response.body();
     SharedPreferencesHelper.setUser(LoginActivity.this, user);
      if (SharedPreferencesHelper.getUserToken(LoginActivity.this) != null){
       Intent i = new Intent(this, MainActivity.class);
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
       startActivity(i);
}
                    
} 

huangapple
  • 本文由 发表于 2020年4月6日 23:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61062756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定