Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persisting Default Preferences.

 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please see the following code in activity class.


In the above code, when the activity runs, it will get the message from sharedprefernces, and if not found, it will put "Message Not Found" in message.

Problem
---------
Let us say, my application is installed for the first time and I want that when the user runs the application for the first time, he should get a valid value. Is there a place where we can set default values for various preferences that can be retrieved on first run of the application. Please let me know.

Thanks.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Message Not Found" is the default, in case no preference has been set yet; what else are you looking for?
 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's correct "Message Not Found" is the default value. But there is a problem. If I have 15 preferences, the default values will be littered everywhere in the code. It would be nice to have a common place like XML, or property file where we define default values and then code in Activity can just use it instead of givign default values in method calls in Activity.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing stops you from keeping them in an XML file like strings.xml or something like it. That is certainly a better approach than to keep the defaults in the code.
 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is how to achieve it, I want to get rid of second argument in the code below completely.....

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
String message = settings.getString("default_message_key", "Message Not Found");

a small code snippet or link to a URL will definitely help.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get rid of second argument in the code below completely.

Why? That's how defaults are generally handled, not just in Android, but elsewhere in the Java world as well.
 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, it means default values will always be there in the code. Ok, still how do we define default values for preferences in XML. I want to learn that
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By keeping them as strings in strings.xml and using them like you would any other string defined in that file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic