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 8675369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:55:58+00:00 2026-06-12T19:55:58+00:00

I’ve logged maybe 6 hours so far fighting with Android preferences, including reviewing and

  • 0

I’ve logged maybe 6 hours so far fighting with Android preferences, including reviewing and trying many stackoverflow suggestions. My latest problem is that changes I make from my preferences activity aren’t carrying over to my main activity. I have distilled the code.

Main activity:

public class MainActivity extends Activity {

    /*...*/

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*...*/
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        super.onOptionsItemSelected(item);
        switch(item.getItemId())
        {
        case R.id.menu_settings:
            startActivity(new Intent(this, PrefsActivity.class));
            return true;
        }
        return false;
    }

    @Override
    public void onStart()
    {
        super.onStart();

        SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
        int somePrefValue = prefs.getInt("somePrefKey", 1);
        /*...*/
    }
}

Preferences activity:

public class PrefsActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.app_prefs);
    }
}

Preferences resource:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="Some Category">

        <EditTextPreference
            android:key="somePrefKey"
            android:title="Some Preference"
            android:defaultValue="1"
            android:numeric="integer" /> 

    </PreferenceCategory>

</PreferenceScreen>

In case it matters, I’m having this trouble in 4.1, both in the emulator and on a Nexus 7.

<VENT>For the record, here are the battles I have so far fought (and won) with preferences:

  1. Where the XML preferences file goes in the Eclipse tree and what to
    call it.
  2. How to force the preferences activity to restrict values to
    integers or floats.
  3. That specifying a preference value as an
    integer or float in the XML does not make the preference an
    integer or float — it’s still a string.
  4. That despite several methods
    taking the name of the shared preferences file, the only method that
    will actually load the file is addPreferencesFromResource(), only
    from PreferenceActivity. I have to code the defaults redundantly in
    the XML and in the first prefs getters, unless I parse the XML.

I had these problems despite reading the official Android documentation for preferences and despite reading the chapters on using preferences in three different books. Android documentation is generally pretty good, but considering my (continuing) trouble and the vast number of stackoverflow questions about this, I think this part needs some work.</VENT>

Thank you for any help you can provide with my latest problem.

P.S. I realize that the non-fragment approach is now deprecated, but I’m new to Android and have a tight deadline for producing a prototype that need not run on anything but my Nexus 7 tablet. I’d prefer it simple, though it’s now looking like I didn’t save any time.

  • 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-12T19:55:59+00:00Added an answer on June 12, 2026 at 7:55 pm

    You seem to be using a non-default SharedPreferences in your main activity "app_prefs", but your PreferenceActivity doesn’t reference this, and is probably storing to the default SharedPreferences.

    You should either:

    1. Use the default SharedPreferences in the Main activity, instead of a named one: (also moving to onResume as suggested by louielouie)

      @Override
      public void onResume()
      {
          super.onResume();
      
          SharedPreferences prefs = 
             PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      
          int somePrefValue = Integer.valueOf(prefs.getString("somePrefKey", "1"));
          /*...*/
      }
      

      or

    2. Tell your PreferenceActivity the name of the SharedPreferences you want to use:

      public class PrefsActivity extends PreferenceActivity {
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              getPreferenceManager().setSharedPreferencesName("app_prefs"); //ADD ME
              addPreferencesFromResource(R.xml.app_prefs);
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.