I have created a SettingsActivity for my app. In this Activity I am using the SharedPreferences class to do handle the user editable preferences.
While setting up the SharedPreferences, I have to load them in the onCreate of my main activity and then again in the SettingsActivity. The probably was that both calls to the getXXXX() methods require defaults and I figured that it would not be good to hard-code the default values into both places because I would imagine it would be problematic in the future if I ever changed them.
Which is the best/most popular (or accepted standard) of doing this?
-
Create a global variables class in which I import into each activity and define my default constants in there?
-
Use
putExtraandgetExtrato pass the data from the main activity to the settings activity?
Any other suggestions?
I think Squonk has a good answer, but if you’re looking for an alternative, consider creating a
Settingsclass with all of your settings as members. It could have a static method likeloadFromPreferences(Context)that would return aSettingsobject constructed fromSharedPreferences, using whatever defaults you need. It could also have asaveSettings(Context)method to save your edits. Hope that helps.