I got some troubles with a SharedPreferences. It looks like they are not shared trough my different activities (for example my preferences are saved in my PreferencesActivity but not available from my other activities)
Here is the code for my PrefencesActivity :
public class SettingsActivity extends SherlockPreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating the ActionBar
ActionBar mActionBar = getSupportActionBar();
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(true);
// Getting the preferences
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PreferencesFile", getApplicationContext().MODE_PRIVATE);
getPreferenceManager().setSharedPreferencesName("PreferencesFiles");
addPreferencesFromResource(R.xml.preferences);
}
}
The preferences.xml file :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Informations utilisateur">
<EditTextPreference
android:key="id_utilisateur"
android:title="Identifiant"
android:summary="Votre identifiant Hangin\'Around"
android:dialogTitle="Identifiant Hangin\'Around :" />
<EditTextPreference
android:key="mdp_utilisateur"
android:title="Mot de passe"
android:summary="Votre mot de passe sur Hangin\'Around"
android:dialogTitle="Mot de passe Hangin\'Around :" />
</PreferenceCategory>
<PreferenceCategory
android:title="Alerte de chute">
<EditTextPreference
android:key="num_contact"
android:title="Numéro à contacter"
android:summary="Le numéro auquel un SMS sera envoyé en cas de chute"
android:dialogTitle="Numéro à contacter en cas de chute :" />
<EditTextPreference
android:key="nom"
android:title="Nom présenté"
android:summary="Le nom auquel vous serez présenté à votre contact"
android:dialogTitle="Nom présenté :" />
<CheckBoxPreference
android:key="confirm"
android:title="Confirmation de le chute"
android:summary="Affichera un message vous demandant de confirmer l'envoi d'un message d'alerte de chute" />
</PreferenceCategory>
<PreferenceCategory
android:title="Gestion des modèles de mouvement">
<PreferenceScreen
android:title="Enregistrer un nouveau modèle">
<intent android:action=".EnregistrementActivity"/>
</PreferenceScreen>
<PreferenceScreen
android:title="Gérer les modèles de mouvement">
<intent android:action=".GestionModelesActivity"/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
And here’s how I get the preferences in my activities :
protected void onCreate(Bundle savedInstanceState) {
//creating the activity and stuffs
// Getting my preferences
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PreferencesFile", getApplicationContext().MODE_PRIVATE);
numContact = preferences.getString("num_contact", null);
nomUser = preferences.getString("nom", null);
confirm = preferences.getBoolean("confirm", false);
modeleChute = preferences.getString("modele_chute", null);
Log.d(TAG, "numContact = " + numContact + " - nomUser = " + nomUser + " - modeleChute = " + modeleChute);
}
And the log shows (modeleChute comes from another activity) :
01-14 17:04:40.444: D/MainActivity(13277): numContact = null - nomUser = null - modeleChute = Immobile
Am I doing something wrong ? (I guess so)
Change
to:
update: The reason is the default implementation of the xml preferences will save to your defaultSharedPreferences rather than the one you defined. You can use
setDefaultValues to change the default if its really needed.