IB. IB. - 4 months ago 7x
Android Question

sharedPreference from helper class

I have an application where i store info data in a sharedPreference
I want to create a static class which will hold the data from the sharedPreference (get and set) so i will not have to call each time to the sharedPreference(performance issues)
The static helper class dont have reference to the sharedPreference, is there any way to get hold of it without passing the calling activity as parameter?
Maybe getParent() ? or getContext() ?


I think you have 2 ways to do it :

  • You can pass a Context in the parameters of all your static methods. Then just use context.getSharedPreferences().

  • Create a static init method where you record the data of your SharedPreferences in static fields of your class.

For my perspective I prefer n°1 because in n°2 you have to call initand modify it each time you add a new data in the SharedPreferences