When my app starts, I populate a container class with values from my shard prefs. The idea was to handle the SharedPreferences and PreferenceManager once since I'm guessing they're heavy.
Here's a sample:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cont);
StorageClass.lifespan = Integer.parseInt( prefs.getString("lifespan", "8") );
StorageClass.hiRate = Integer.parseInt( prefs.getString("hiRate", "71") );
//and on and on for all preferences
Other activities then request these values.
But as my app grows, main memory is becoming tight.
Would it be better to have every requester make an instance of SharedPreferences and get the value they want?
Thanks
The same SharedPreferences
object is returned each time you request the preferences of the same name. If that wasn't the case, there would need to be a lot of complicated code inside the framework to deal with multiple SharedPreference
objects keeping their state in sync when it is changed by one of them.
(This is kind-of the idea behind calling it "shared" preferences.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With