VolodymyrH VolodymyrH - 1 month ago 8
Android Question

Do previous Loaders die or will they be stored in memory if I create new ones?

I have a Loader which downloads data for my RecyclerView. I have added SwipeReftesh to this list and I can't figure out what will be if I swipe down, for example, for a few times. Do previous Loaders die or will they be stored in memory? It looks like this:

refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
initializeDiscoverLoader();
refreshLayout.setRefreshing(false);
}
});


private void initializeDiscoverLoader() {
LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(R.integer.MOVIE_LOADER_DISCOVER_ID, null, MovieActivity.this);
}

Answer Source

RefreshLayout just displays the progress indicator and calls your app's callback method. What you do inside your callback method has no relation to RefreshLayout. So if you have any previous loaders already running, they will keep running until they finish.

It is always good to use same loader object to load your data. This way, only one loader object will run at any given time. You won't have many loader objects running simultaneously.

Also, you have used refreshLayout.setRefreshing(false) inside onRefresh() which is not needed as Android calls it itself when onRefresh() has executed completely.

Reference - From Documentation -

  1. When the user makes a swipe gesture, the system displays the progress indicator and calls your app's callback method. Your callback method is responsible for actually updating the app's data.

  2. When the user triggers a refresh with a swipe action as described in Respond to the Refresh Gesture, you do not need to call setRefreshing()