dimanche 26 juin 2016

Value in SharedPreference cached in Android 6

I use SharePreference to save username after user login to my app and clear it if user logout.

I will check that value when start app, if that value null user need login to use.

However when I check with android 6.0:

Login (save username - Toast show not null) > Logout (remove username - Toast show null) > Remove App > Reinstall app > StartApp (not login - Toast show not null, this value is user that used => issue)

This issue no occur with android < 6.0

Here is my code:

init sharepreference in Application:

public static SharedPreferences getPref() {
    return mInstance.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}

Check to login:

if (App.getPref().getString("username", null) == null){
   //Login
}

Save if login success:

                App.getPref().edit()
                                .putString("username", s.getLoggedInUser().getUsername())
                                .putString("profilePicUrl", s.getLoggedInUser().getProfilePicUrl())
                                .putString("pk", s.getLoggedInUser().getPk())
                                .apply();

Clear when logout:

   App.getPref().edit().clear().apply();

Aucun commentaire:

Enregistrer un commentaire