Reputation: 313
As far as I know the getDefaultSharedPreferences
is loading all the preference file into memory.
In my app I have many classes where I pass the context and use getDefaultSharedPreferences
. During the execution these classes load so many times, then as a result getDefaultSharedPreferences
is called allot.
My question is: Should I load the preferences only one time in Application class and then access the preferences from there in all the classes? Is this doable? Will this increase the speed of my app? Anybody did this?
Something like this:
private static MyApplication singleton;
public static MyApplication getInstance() {
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
myPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}
public SharedPreferences getPreferences(){
return myPreferences;
}
Upvotes: 1
Views: 879
Reputation: 7214
SharedPreferences
caches after first load, so disk access to load data will take time but for only one time.Once they are in memory, after the first reference. The first time you retrieve a specific SharedPreferences
(e.g., PreferenceManager.getDefaultSharedPreferences()
), the data is loaded from disk, and kept around.
Upvotes: 1