当用户单击注销时,我尝试从我的应用程序中注销。如果用户在登录后没有关闭应用程序,如果他注销了,它工作正常。然后它工作正常。返回按钮将无效,以防再次显示登录后的登录页面但是当用户登录后关闭应用程序然后他注销登录页面没有显示它向我显示一个空白页面
代码
public class AppState {
private static AppState singleInstance;
private boolean isLoggingOut;
private AppState() {
}
public static AppState getSingleInstance() {
if (singleInstance == null) {
singleInstance = new AppState();
}
return singleInstance;
}
public boolean isLoggingOut() {
return isLoggingOut;
}
public void setLoggingOut(boolean isLoggingOut) {
this.isLoggingOut = isLoggingOut;
}
}
OnClick 注销
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences myPrefs = getSharedPreferences("MY",
MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
AppState.getSingleInstance().setLoggingOut(true);
Log.d(TAG, "Now log out and start the activity login");
Intent intent = new Intent(HomePage.this,
LoginPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
在登录活动中
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MAIN_ACTIVITY_REQUEST_CODE) {
if (!AppState.getSingleInstance().isLoggingOut()) {
finish();
} else {
AppState.getSingleInstance().setLoggingOut(false);
super.onActivityResult(requestCode, resultCode, data);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
请告诉我我在这方面做错了什么
在您对Vivek Bhusal 提出建议后,我尝试使用 sharedpref
主页注销点击
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences myPrefs = getSharedPreferences("Activity",
MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
//AppState.getSingleInstance().setLoggingOut(true);
setLoginState(true);
Log.d(TAG, "Now log out and start the activity login");
Intent intent = new Intent(HomePage.this,
LoginPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
private void setLoginState(boolean status) {
SharedPreferences sp = getSharedPreferences("LoginState",
MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean("setLoggingOut", status);
ed.commit();
}
在登录页面上
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences sp = getSharedPreferences("LoginState",
MODE_PRIVATE);
boolean stateValue = sp.getBoolean("setLoggingOut", false);
if (requestCode == MAIN_ACTIVITY_REQUEST_CODE) {
if (!stateValue) {
finish();
} else {
//AppState.getSingleInstance().setLoggingOut(false);
updateLoginState(false);
super.onActivityResult(requestCode, resultCode, data);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
当我再次重新启动应用程序然后注销时仍然显示空白屏幕的相同问题。