Reputation: 145
I am making an app that has a list of books that the user has, and I need to access it from multiple activities and fragments. I have a two problems:
List<Book>
around to each activity and its very annoying.I would use shared preferences, but they can't hold Parcelable
es. I did see a library that lets you store data as strings by converting them them to json using Gson, but i'm affraid that will be too performance intensive. So summing up, where do I put data that needs to be accessed by all activities and be stored permanently?
Upvotes: 1
Views: 42
Reputation: 1
you cat use singltone something like this
public class Data {
public Boolean m_b1 = false;
public Boolean m_b2 = false;
private List<String> m_oMyList;
private static Data x;
private Data() {
m_oList = new ArrayList<String>();
}
public static Data X()
{
if(x == null)
x = new Data();
return x;
}
}
Upvotes: 0
Reputation: 5840
For managing data that can be accessed from all activities i recommend using some kind of AppResources class which holds static variables (like your books List) that can be used by any app activity, fragment (In case the Context is needed just pass the Context to static to these methods).
Converting data to strings is not expensive in terms of performance, and so does storing data in the Shared Preferences which loads the data once to a map and afterwards each get or set takes < 1ms
Upvotes: 0
Reputation: 5737
There are some solutions:
1. You can use a server side services to store the data there (if you have server).
2. You can store the books list in database or to use GSON and store it as JSON string in Shared Preferences.
3. you can extend the Application class.
All the solutions above sole the accessibility problem.
The performances depends on your data size.
You can also try to use less Activities and more Fragment so you need less database/ network calls.
Upvotes: 1