My app downloads JSON data from Internet and displays it as a RecyclerView list. This is implemented through LoaderManager/Loader means. Action is initiated by clicking a button.
In onCreate I call initLoader() if that specific loader already exists, in order to give it new callbacks, bound to the new Activity (I do this to ensure user doesn't lose progress if he rotates the screen mid-download). Now I have arranged for organized list data to be retained upon screen rotation. For example, if I load data and then rotate the screen, that call to initLoader() will correctly deduce the data had already been loaded and will call onLoadFinished() (I suspect loaded data is cached somehow internally).
What baffles me is that loadInBackground() will somehow be called also, absolutely needlessly.
Reading documentation didn't quite clarify things for me.
How do I merely pass new callbacks to my loader upon onCreate() without needless operations being called?
Edit: Here is AsyncTaskLoader code.
@NonNull
@Override
public Loader<String> onCreateLoader(int id, final Bundle args) {
return new AsyncTaskLoader<String>(this) {
@Override
protected void onStartLoading() {
Log.w("DBG", "onStartLoading");
if (args == null) return;
Log.w("DBG", "about to forceLoad");
forceLoad();
}
@Override
public String loadInBackground() {
Log.w("DBG", "loadInBackground");
String baseId = args.getString("baseId");
String quoteId = args.getString("quoteId");
String exchange = args.getString("exchange");
Uri uri = buildUri(baseId, quoteId, exchange);
String txt = uri.toString();
Log.i("Uri built: ", txt);
String response = "";
try {
URL url = new URL(uri.toString());
response = getResponseFromHttpUrl(url);
} catch (IOException e) {
e.printStackTrace();
}
final int PAUSE_SEC = 5;
try {
Thread.sleep(PAUSE_SEC * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return response;
}
};
}