Search code examples
androidmobileandroid-sharedpreferences

Writing in two different SharedPreference and getting the same result


I'm having an issue when I'm trying to write in two differents string key-value while programming on Android. I have the same value for two differents key.

SharedPreferences idUserPref = getSharedPreferences(getString(R.string.id_utilisateur), Context.MODE_PRIVATE);
SharedPreferences estConnectePref = getSharedPreferences((getString(R.string.estConnecte)), Context.MODE_PRIVATE);
SharedPreferences.Editor estConnecteEditor = estConnectePref.edit();
SharedPreferences.Editor userIdEditor = idUserPref.edit();
userIdEditor.putString(getString(R.string.id_utilisateur), result);
estConnecteEditor.putString(getString(R.string.estConnecte), "vrai");
userIdEditor.commit();
estConnecteEditor.commit();
String utilisateurId = idUserPref.getString(getString(R.string.id_utilisateur), "Invalid");
String estConnecte = estConnectePref.getString(getString(R.string.estConnecte), "faux");
Log.d("ConnexionActivity", "Id User : "+utilisateurId);
Log.d("ConnexionActivity", "est connecté : " + estConnecte);

and the result in Logcat is :

08-19 18:25:27.005 3882-3882/com.partenaires.legimetrie.legimetrieapp D/ConnexionActivity: Id User : vrai
08-19 18:25:27.005 3882-3882/com.partenaires.legimetrie.legimetrieapp D/ConnexionActivity: est connecté : vrai

I don't understand why. Does someone can help me ?


Solution

  • FYI: Share Preference store values in xml format with name of share Preference.

    So to store value with different share Preference you must use different name for share Preference.

    so R.string.id_utilisateur and R.string.estConnecte must be different name.