Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:15:57+00:00 2026-06-17T14:15:57+00:00

I got some troubles with a SharedPreferences . It looks like they are not

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:15:58+00:00Added an answer on June 17, 2026 at 2:15 pm

    Change

    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PreferencesFile", getApplicationContext().MODE_PRIVATE);
    

    to:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really have some troubles since 1 week ago. I got errors like this
I've got some some troubles with Primefaces 3.2 and JSF 2.1. My Code like
I got some troubles trying to copy char to char... Here's args[] definition :
So I've got some troubles with character encoding. When I put the following two
I have just installed Delphi 2010 and got some troubles with idHTTP component. If
I am creating a MVC4 application and I have got some troubles when I
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
I begin in Ios dev and I got some troubles to manipulate an array
I've querying an API and I've got some troubles to display the code returned
I am trying to make a listview in android but i got some troubles.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.