Ok so I'm trying to keep a singleton instance of GetItemQuery
which is a POJO to hold the requests and response fields needed for Retrofit API calls. I dont need to create a new GetItemQuery
every time I make a call, hence I'm using the singleton pattern to set the fields of the existing GetItemQuery
instance.
public class GetItemQuery {
// request data
private String itemId;
private int numberToLoad;
// response data
private Item item;
private static class Loader {
static final GetItemQuery sInstance = new GetItemQuery();
}
public static GetItemQuery getInstance(Item item) {
GetItemQuery instance = Loader.sInstance;
instance.setItem(item);
return instance;
}
public Item getItem() {
if (item == null) {
item = new Item();
}
return item;
}
public void setItem(Item item) {
this.item = item;
}
// other getters and setters
}
in my Android app. At what points should I be worried that getInstance()
will be garbage collected so all the fields are cleared? Or is there a way for me to manually garbage collect so I ensure some fields don't become null unexpectedly?