Вопрос

I have ListFragment and AsyncTaskLoader which fetches data from internet. ListFragment is filled by BaseAdapter.

When data is loading, BaseAdapter fills the first row with spinning progress bar. When data is loaded and no errors occured, data is filled into ListView. But if some error occured(I have a flag in AsyncTaskLoader indicating that error), then BaseAdapter fill the first row with error message.

Problem:

When loading has errors, LoaderManager automatically tries to load data again(forceLoad() called), which is good. But what I need is to notify BaseAdapter that data is loading so BaseAdapter can change error message to spinning progress bar.

Is there any easy good way to notify fragment when LoaderManager is calling forceLoad()?

My suggestion:

ListFragment will implement the interface below and pass this ListFragment in AsyncTaskLoader's constructor.

public interface LoadingListener {
    public void onLoadingStatusChanged(boolean isLoading);
}

Then I can call onLoadingStatusChanged(true) in loadInBackground() and onLoadingStatusChanged(false) in deliverResult(T data). I am affraid that holding reference to Fragment in AsyncTaskLoader may leak that fragment.

Это было полезно?

Решение

Your suggested implementation is a fine way to handle things. Although Loader already presents callbacks for OnLoadComplete and OnLoadCanceled, so a started callback is the only addition you need to make. You could also implement a custom OnLoadStart callback that triggers anytime onForceLoad() or onStartLoad() is called as opposed to loadInBackground and it would more closely mirror the existing framework callbacks.

I would suggest that, just like the framework callbacks, you set up methods to register and unregister the listener rather than passing it in the constructor, so your Fragment can attach/remove itself as part of the normal lifecycle and the reference doesn't leak. As you mentioned, passing it in the constructor with no way to remove it would be considered a leak.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top