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
posted
0
"Message Not Found" is the default, in case no preference has been set yet; what else are you looking for?
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
posted
0
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
posted
0
The problem is how to achieve it, I want to get rid of second argument in the code below completely.....
a small code snippet or link to a URL will definitely help.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32769
posted
0
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
posted
0
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
posted
0
By keeping them as strings in strings.xml and using them like you would any other string defined in that file.