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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:09:50+00:00 2026-05-24T07:09:50+00:00

In order to initialize preferences with default values from XML file describing the preferences,

  • 0

In order to initialize preferences with default values from XML file describing the preferences, I can call PreferenceManager.setDefaultValues(this, R.xml.preference, false). Sounds simple, but I’m not quite sure when exactly should I call this?

As I understand from the docs, the above call is only needed once, in situation when no preferences are set yet. As a result of this call, preferences residing in /data/data/<myapp>/shared_prefs are going to be set, so all subsequent attempts to read preferences will get me the default values. Logically, setDefaultValues should be called in every single code path that might be executed without preferences being already initialized. Over time, this turned out to be multiple places – main activity, another activity, background service, small BroadcastReceiver handling system messages… Right now I’ve put call to setDefaultValues in onCreate() for my Application object, as I’m already using it as convenient singleton for other things.

Questions:

  • Do I have a guarantee that every time my code executes, Application object will be created and onCreate will run?
  • How are you dealing with this problem? One other way would be to hardcode default values into getFoo(key, defValue) calls, but that effectively scatters your default settings across whole code.

EDIT: Essentially, I don’t know which solution is worse: calling setDefaultValues every time I access prefs in given code path, or calling it in some common place (like app’s onCreate) every time, no matter whether I need it or not.

  • 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-24T07:09:52+00:00Added an answer on May 24, 2026 at 7:09 am

    I’m going to delete my original answer and answer the questions that you actually asked.

    1. Yes, the Application object’s onCreate will be executed at the start of every process. Keep in mind that doesn’t guarantee it will be run each time you start your main activity. If Android still has your process running it will use that again (e.g. you still have a service running). So yes what you’re doing will work and you’re correct in observing it won’t blow up.
    2. I’m dealing with this problem by subclassing SharedPreferences (let’s call it MyPrefs — that’s not what I call it but that’s not important). The key features of MyPrefs are:
      1. encapsulation of get/set methods instead of directly accessing the key names
      2. Handling code for loading defaults. I’m being a little lazy by using a static boolean instead of an AtomicBoolean to tell me if the defaults have been loaded.

    Having said that… it works for me, but if you’re almost certain you’ll be calling the SharedPreferences every time your code runs where you’re at works as good as any.

    Hope this helps more than my previous answer.

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

Sidebar

Related Questions

Can I initialize the cropping area (In order to suggest a default one), with
I've got this JS code, in the same shown order: function init(){ //Initialize stuff
In order to initialize all JTextfField s on a JPanel when users click a
I'm doing an INSERT INTO query in order to initialize a new table. The
C++ guarantees that variables in a compilation unit (.cpp file) are initialised in order
Just seen an interesting possibility to initialize code blocks in Scala for high order
In order to implement auto-vivification of Ruby hash, one can employ the following class
If I have Order entity with a list of OrderDetails I can easily eager
Is it possible to force Ruby to call an initialize method when using YAML.load_file?
I use custom model binder to bind Order/OrderItem in my actions. This model binder

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.