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

  • Home
  • SEARCH
  • 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 3317400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:34:59+00:00 2026-05-17T22:34:59+00:00

I have a PreferenceActivity that loads its preferences from an XML file which looks

  • 0

I have a PreferenceActivity that loads its preferences from an XML file which looks like this

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="first_preferencescreen">
   <ListPreference
 android:key="currlist"
    android:title="Change Currency"
    android:summary="Selecting a new currency will reset your jar!"
    android:entries="@array/currencies"
    android:entryValues="@array/currency_vals" />
    <EditTextPreference
    android:key="goaltxt"
    android:title="Set Goal"
    android:inputType="numberDecimal"
    android:text="0.00"
    />
</PreferenceScreen>

I want to pass both the currency type and the goal back to the main class, and all the explanations I’ve found are very vague. In my onStart() method in my main class I have this so far:

public void onStart() {
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
  final SharedPreferences.Editor prefsEditor = myPrefs.edit();

  prefs.getInt("currlist", 0);
  prefs.getFloat("goaltxt", 0);


 }

This is really frustrating, and I’d love some insight on how SharedPreferences works. Thanks!

  • 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-05-17T22:35:00+00:00Added an answer on May 17, 2026 at 10:35 pm

    SharedPreferences are a way of sharing data between many parts of your application. Once someone has a reference to a shared preference they can read things from it like so:

    int currlist = prefs.getInt("currlist", 0);
    float goal   = prefs.getFloat("goaltxt", 0);
    

    They can also register to receive notifications that the data has changed:

    prefs.registerOnSharedPreferenceChangeListener(this);
    

    Their onSharedPreferenceChanged method will fire when someone changes the data in the shared preferences (they must implement the SharedPreferenceChangedListener interface of course). Remember to unregister once you’re no longer observing the data set.

    How does it know when the preferences have changed? Well, you must get a reference to a SharedPreferences.Editor before you can edit the data inside the SharedPreferences, like you have done:

    final SharedPreferences.Editor prefsEditor = prefs.edit();
    

    At this point you use the editor to add or update some variable stored in the shared preferences:

    prefsEditor.putInt("currlist", 1);
    

    And when you’re done editing you commit your changes, this will notify all the listeners that a change has happened:

    prefsEditor.commit();
    

    At this point, all the registered observers will be notified that there has been a change to the shared preferences (including your own!)

    How to put this to use:

    1. When your application, activity, service or whatever starts, grab a reference to the shared preference you’re interested in, copy all the values you need into class member variables and register yourself as an observer.

    2. Remember to remove the listener when your class is no longer active and/or listening to change events.

    3. In your onSharedPreferenceChanged method, check to see if the value in prefs.getWhatever("value", default) is different from your class member variable. If it is, take action but be aware that this is probably being called from another thread; you can’t edit the UI from outside a UI thread or things will crash, similarly you don’t want to cause delays on the UI thread or it will hang. Do very fast, local work only (add stuff to queues etc) and be sure to write threadsafe code.

    4. When you change some value which is shared, first change your class member variable, then grab a prefs editor, make the change to the variable and commit() the result. Your local onSharedPreferenceChanged listener will fire, but as the value is the same as your member variable you will ignore it.

    From this point on your inter-class communication is solid, make a change to the prefs and the UI seamlessly updates, send an update to running services, pass values around between unrelated activities.

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

Sidebar

Related Questions

I have an activity which extends PreferenceActivity. I'm loading preferences from the xml file.
I have my own custom Content Provider that loads a database which contains the
I have a PreferenceCategory , xml file and I have defined all preferences in
I have pretty unassuming preferences screen based on PreferenceActivity . (You can see it
I have a ListView that has some minor visual preferences that are set in
I have the following code in my application in res/xml/preferences.xml: <PreferenceScreen xmlns:android=http://schemas.android.com/apk/res/android> <PreferenceCategory android:title=Wi-Fi
I have created a PreferenceActivity based on xml for PreferenceScreen. In the xml you
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the

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.