在我的MainActivity
中,我首先检查是否有人登录。如果没有,则会将用户发送到LoginActivity
。如果有人登录,我会检查令牌是否有效,如果没有,则再次将用户发送到LoginActivity
。如果令牌有效,我会检查用户是否已完成他/她的个人资料的详细信息。如果没有,则会将其发送到ProfileDetailsActivity
。如果有,则代码应该继续,用户应该在MainActivity
。我的问题是,当用户登录时,一切都按预期进行。由于我的测试用户尚未完成个人资料详细信息,因此我被发送到ProfileDetailsActivity
。但是,如果我完全关闭应用程序并重新打开它,则用户将保持MainActivity
并且永远不会被发送到配置文件详细信息页面。这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Backendless.initApp(this, BackendSettings.APPLICATION_ID, BackendSettings.SECRET_KEY, BackendSettings.VERSION);
String userToken = UserTokenStorageFactory.instance().getStorage().get();
showProgressDialog();
if (userToken == null || userToken.equals("") {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
AsyncCallback<Boolean> isValidLoginCallback = new AsyncCallback<Boolean>() {
@Override
public void handleResponse(Boolean aBoolean) {
if (!aBoolean) {
Backendless.UserService.logout(new AsyncCallback<Void>() {
@Override
public void handleResponse(Void aVoid) {
Toast.makeText(MainActivity.this, BackendSettings.ERROR_SESSION_EXPIRED, Toast.LENGTH_SHORT).show();
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
Intent intent = Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
BackendlessUser user = Backendless.UserService.CurrentUser();
if (user != null) {
boolean completeProfile = (boolean) user.getProperty("complete_profile");
if (!completeProfile) {
Intent intent = new Intent(MainActivity.this, ProfileDetailsActivity.class);
startActivity(intent);
finish();
}
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
};
Backendless.UserService.isValidLogin(isValidLoginCallback);
}
hideProgressDialog();
}
我也尝试在hideProgressDialog
之后移动此代码块,但仍然没有好处:
BackendlessUser user = BackendlessUser.UserService.CurrentUser();
if (user != null) {
boolean completeProfile = (boolean) user.getProperty("complete_profile");
if (!completeProfile) {
Intent intent = new Intent(MainActivity.this, ProfileDetailsActivity.class);
startActivity(intent);
finish();
}
}