0

I am working on an android application where by a user will need to register/login, this works well however when the user goes to log out is where the issue is.

Currently when the user presses the logout button it does take them back to the login page, however without logging in again if you navigate away from the app and then come back to it (so its still in memory) it takes you back to the page you see after logging in.

I have used a sharedpreference to record if a user is logged in or not and then have a splash activity which is the first thing to start to decide which screen to show:

public class Splash extends SherlockActivity {
    public static final String PREFS_NAME = "PrefsFile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean loggedin = settings.getBoolean("loggedIn", false);
        if (loggedin){
            Intent intent = new Intent(this, MyLists.class);
            startActivity(intent);
        }
        else{
            Intent intent = new Intent(this, LogIn.class);
            startActivity(intent);
        }
    }

}

And then my logout button looks like

private OnClickListener OnClick_logout = new OnClickListener() {
        public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("loggedIn", false);
            editor.putString("email", "");
            editor.putString("password", "");
            editor.commit();
            db.clearLists();
            db.clearProducts();
            Intent intent = new Intent(v.getContext(), Splash.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            v.getContext().startActivity(intent);
        }
    };

after the button is pressed the splash activity takes the user to the login screen currently but like I say if you then close the app and come back to it it will take the user to the 'MyLists' activity.

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101

1 Answers1

0

You need to use intent flags for clearing stack history. Before you navigate back to login screen, say

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

or

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Then on backbutton click, your app exits.

Also ensure, you flush the sharepreference variables to zero or call System.exit().

P.S : calling system.exit() will make the user login each time he/she wants to use the app.

Hope that helps

Rashmi.B
  • 1,787
  • 2
  • 18
  • 34