I'm trying to create a function that globally changes a setting which is represented by a SharedPreference and can later be manually set for each activity. In the settings, I want to offer the user the ability to change the setting for every activity at once. Is there a way to get a list of Activity Contexts in a static function?
Asked
Active
Viewed 115 times
0
-
why don't you use preference Activity for that? – V-rund Puro-hit Aug 08 '16 at 06:12
-
In this case I don't use a preference activity because the setting has a unique preference for each activity it is used from. I need a way to change them all at once programmatically. – Mr. Negi Aug 08 '16 at 06:19
-
Does all activities in same application? If yes, simply make each activity a singleton and make a static method like void refresh(), and put all activities refresh method in one method; everytime, you just call that method to refresh all activities – S.W. Aug 08 '16 at 06:22
1 Answers
1
You can keep them all in the global preferences, just append the activity class name to the Preference key (like "activity_color_global", "activity_color_view"...), or use a different preference file name for each Activity.
By default set the value of all keys to 0 or null, which would mean to use the global preference (the one with the global name appended), and then user can change each one separately if they want.
To dynamically get a list of all included Activities, you can use the getPackageInfo
function from the PackageManager
class.

lionscribe
- 3,413
- 1
- 16
- 21
-
I appreciate the answer, but this is not answering my question. I considering using this approach, but given the fact that the number of activities in my project changes frequently during development, it would be much better design to rely on a dynamic list of all the possible activity contexts than a manually defined set of strings. – Mr. Negi Aug 08 '16 at 06:29
-
1You can use getPackageInfo from the PackageManager to dynamically get a list of included Activities. – lionscribe Aug 08 '16 at 12:56
-
Thanks! This is exactly what I was looking for. If you change main answer to this I'll accept it. – Mr. Negi Aug 12 '16 at 02:24