IntelliJ Java IDE
The moose likes Android and the fly likes Persisting Default Preferences. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "Persisting Default Preferences." Watch "Persisting Default Preferences." New topic
Author

Persisting Default Preferences.

Rakesh Jhamb
Ranch Hand

Joined: Jun 18, 2003
Posts: 154
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.


SCJP2, SCWCD
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 32769
"Message Not Found" is the default, in case no preference has been set yet; what else are you looking for?


Android appsImageJ pluginsJava web charts
Rakesh Jhamb
Ranch Hand

Joined: Jun 18, 2003
Posts: 154
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
Marshal

Joined: Mar 22, 2005
Posts: 32769
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

Joined: Jun 18, 2003
Posts: 154
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
Marshal

Joined: Mar 22, 2005
Posts: 32769
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

Joined: Jun 18, 2003
Posts: 154
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
Marshal

Joined: Mar 22, 2005
Posts: 32769
By keeping them as strings in strings.xml and using them like you would any other string defined in that file.
 
IntelliJ Java IDE
 
subject: Persisting Default Preferences.
 
Threads others viewed
A question about Exception in mock exam
Could not create system prefs directory
Just finished my first java program :-) Can you please give me suggestions on making my code better?
Portlet Preferences not loading , portlet does not populate table values.
Problem in starting activity from broadcast receivers
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com