I have simple DataHolder
class:
public class DataHolder
{
private static final DataHolder holder = new DataHolder();
Map<String, WeakReference<Object>> data = new HashMap<>();
public static DataHolder getInstance() {return holder;}
public void save(String id, Object object)
{
data.put(id, new WeakReference<>(object));
}
public Object retrieve(String id)
{
WeakReference<Object> objectWeakReference = data.get(id);
return objectWeakReference.get();
}
}
Basically, I want to hold some data to retrieve them anywhere else (for example, other activity or fragment).
To save:
DataHoler.getInstance().save("list", myList);
To retrieve data:
List<MyObject> list = (ArrayList) DataHoler.getInstance().retrieve("list");
My intent is to avoid passing large amount of data with parcelable (this should be avoided, list may be large) + simplify access to data between all activities/fragments.
Is my approach correct? Not sure if it won't cause any unexpected behaviours or memory leaks.