I have tried to logout from my app when the user clicks on Logout.It is working fine in case the user after login without closing the app if he doed logout .Then it is working properly.Back button will be ineffective in case to show again the login page after doing login But when the user after login closes the application and then he does logout the login page is not showing it is showing a blank page to me
Code
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 of logout
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);
}
});
IN the LoginActivity
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);
}
Please suggest me what i have done wrong in this
After Your Suggestions of Vivek Bhusal i tried to use sharedpref
HomePage logout Clicked
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();
}
On the Login Page
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);
}
}
Still the same issue showing a blank screen when i again restart the app and then do the logout.
You can use the
SharedPreferences
class in android. It can use to save the value and load it again. This is the docs and tutorial.case R.id.drawer_item_logout: //name of logout button
The
onNewIntent()
should be used insingleTop
activities. Beside that, I don't see astartActivityForResult()
call to Login Page/Activity.Blank page means your activity has not setup its view (with
setContentView()
).I would suggest moving your "logging out" checks and
setContentView()
toonResume()
of the Login Page/Activity.This code is used for logout from app
I noticed that your logout button does start the Login activity but does not finish the Home Page activity. To close the Home Page:
To open it, from the Login Page:
Finish one activity after you start another. This way your task stack will have only one activity, i.e., no back history. Check the example app posted by Vivek Bhusal.
I suggest reading this SO question, the accepted answer, and also this answer for more ideas on how to perform logout and some caveats with
Intent.FLAG_ACTIVITY_CLEAR_TOP
, which, in your case, is not really necessary.do you think this statement correct?? because i saw when this condition is true you finish the activity i think that means logout but isLoggingOut should be true however you check if it is false you call finish. am i right??