Javi Javi - 14 days ago 10
Android Question

Up navigation reloads loader but back navigation doesn't

I am using an AsyncTaskLoader to load data into an ArrayList coming from an HTTPRequest. Once loaded, data is displayed via recycler view as a list. When I click on an item of the list, an activity B gets triggered showing a detailed screen for that data. Then, I have two options to come back to the list, one is via back button (phone), and the other is via the up button <- on the toolbar since avtivity B it has

android.support.PARENT_ACTIVITY
implemented.

Well, back button doesn't trigger the loader, but upbutton reloads the whole thing. What is going on? I want both to behave the same, that is, not reload as I specify in
onStartLoading()
.

This is my AsynTask loader which is called as usual, by implementing the
LoaderCallbacks<List<T>>
interface

public class FallaLoader extends AsyncTaskLoader<List<Falla>> {
private String mUrl;
private List<Falla> mFalla;

FallaLoader(Context context, String url)
{
super(context);
mUrl = url;
}
@Override
protected void onStartLoading()
{
if (mFalla == null) {
// we have no data, so kick off loading
forceLoad();
}
else {
// use cached data, fallas won't change for a year, so... just needed everytime I start
deliverResult(mFalla);
}
}

// This happens in the Background thread
@Override
public List<Falla> loadInBackground()
{
if (mUrl == null)
{
return null;
}
// Perform the network request, parse the response, and extract a list of earthquakes.
// pass the context since it will be needed to get the preferences
return Utils.fetchFallasData(mUrl, getContext());
}

@Override
public void deliverResult(List<Falla> data)
{
// We’ll save the data for later retrieval
mFalla = data;
super.deliverResult(data);
}}


In
onCreate
of Activity A, I have the call to loader like this

`LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(0, null, this);

and then, I implement the interface:

@Override
public Loader<List<Falla>> onCreateLoader(int i, Bundle bundle)
{
return new FallaLoader(this, F_URL);
}

@Override
public void onLoadFinished(Loader<List<Falla>> loader, List<Falla> fallas)
{
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
mEmptyStateTextView.setText(R.string.no_fallas);

if (fallas != null && !fallas.isEmpty())
{
adapter.swap(fallas);
}
}

@Override
public void onLoaderReset(Loader<List<Falla>> loader) {

}


`

Thanks!

Answer

When you come back from activity B, onStartLoading will be called again as loader is aware of the activity states.Now when you press back button of the phone, activity will be simply brought to front but if you press back button in toolbar, previous activity will be created again, hence your loader will be reinitialized and if (mFalla == null) will become true, resulting in call to forceLoad().

You can handle your toolbar's back button click explicitly in activity B to avoid this behavior.

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            onBackPressed(); 
        }
        return true;
    }
Comments