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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:02:17+00:00 2026-06-11T04:02:17+00:00

Android Guide recommends defining preferences in XML files, And from there, these can be

  • 0

Android Guide recommends defining preferences in XML files, And from there, these can be loaded in PreferenceActivity/PreferenceFragment etc for viewing and editing by user. But in real scenario, User Interacts with other activities first, then (maybe) with Preferences UI.

What if the starter activity needs some of these preferences ? They’ll be not loaded yet, because preferences resources has not been inflated yet. Is there a way to pre-access preferences in XML files ?

  • 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-11T04:02:19+00:00Added an answer on June 11, 2026 at 4:02 am

    Yes. When you first request the preference you can provide it with a default value. E.g. if you are loading a preference of type Int, then you can do so in the following manner from an activity:

    SharedPreferences defaultSettings = PreferenceManager.getDefaultSharedPreferences(this);
    int preferenceValue = defaultSettings.getInt("PreferenceName", 7);
    

    This would load your preferenceValue to be 7 (without this preference ever being initialized yet). This is assuming that in your XML preference file, you have a preference of key “PreferenceName”. If you plan on editing this preference in the activity before the Preference activity has been ran, be sure you commit your changes with a SharedPreferenceEditor:

    // ... change to preferenceValue occurs prior to this code
    SharedPreferences.Editor defaultEditor = defaultSettings.edit();
    defaultEditor.putInt("PreferenceName", preferenceValue);
    defaultEditor.commit();
    

    We probably want to avoid “PreferenceName” in a hardcoded matter though, and instead use it as a string in the strings.xml file. This way it can be grabbed both from the initial code when the preference has not been saved yet and from the Preference XML file as well. This means that our above code would substitute the string “PreferenceName” with something like the following:

    getResources().getString(R.string.pref_name)
    

    And in your Preference XML file you may would reference the key in the following way:

    android:key="@string/pref_name"
    android:defaultValue="7"
    

    This should cover “pre-loading” the preference as well as trying to keep most of the application settings within one place. There may indeed be overlap in terms of whether or not the XML preference was created/loaded before the initial Activity occurred, but I haven’t tested that out yet.

    EDIT: It turns out instead of using the above code, you can directly load the XML file (with its default preference) by the following method:

    PreferenceManager.setDefaultValues(this, R.xml.preference, false);
    

    More information about this method can be found in the documentation for the PreferenceManager: http://developer.android.com/reference/android/preference/PreferenceManager.html

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

Sidebar

Related Questions

The documentation ( http://developer.android.com/guide/topics/manifest/manifest-element.html#uid ) only states I can't use raw strings and the
From Android widget screen guidelines, http://developer.android.com/guide/practices/ui_guidelines/widget_design.html , we know that, home screen has 4*4
I'm learning about using Custom Views from the following: http://developer.android.com/guide/topics/ui/custom-components.html#modifying The description says: Class
http://developer.android.com/guide/basics/what-is-android.html See Android Architecture. Can we consider different managers like facade objects for different
From http://developer.android.com/guide/topics/fundamentals.html : It's possible to arrange for two applications to share the same
According to http://developer.android.com/guide/practices/screens_support.html Normal screen with hdpi can have few resolutions, like 480X800 600x1024
I read the http://developer.android.com/guide/practices/screens_support.html.com ? but couldn't get how to apply this, can anyone
I'd like to use the backup agent for saving user preferences: http://developer.android.com/guide/topics/data/backup.html In the
have a look at figure 2. in the link given below.. http://developer.android.com/guide/practices/screens_support.html .. there
Extract from Android Developer Guide link above: 320dp: a typical phone screen (240x320 ldpi,

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.