Reputation: 16619
I need to cache an object to access it every and each Activity
and Fragment
in my app.
I tried to make it static
, But , unfortunately, sometimes Android OS makes this static as null which crashes my app with no reason.
so is it a good practice to use SharedPreferences
with Gson
to serialize and
cache the object on the disk?
Upvotes: 1
Views: 2377
Reputation: 6968
sharedPreferences
are designed to store data which should be retained between sessions.
If you need that object between the app launches then it's ok to use sharedPreference.
For your problem sharedPreference is a quick solution, however I still encourage you to find reason of your static variable being null.
Upvotes: 1
Reputation: 17899
Generaly, you shouldn't use too much the SharedPreferences
as it's not as fast as a local database, but actually, you can use it if your set of data is not too big and if you just want a simple cache.
A simple alternative would be to use Realm.
Upvotes: 3