英文:
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);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论