Juan Pedro Martinez
Juan Pedro Martinez

Reputation: 1974

AsyncTaskLoader is not working properly on fragment

I am trying to implement an AsyncTaskLoader running in a fragment and i don't know exactly the reason why onLoadFinished never is called. I am not sure if the context that i pass is the proper one.

This is the basic and custom AsyncTaskLoader:

public static class CustomAsyncLoader extends AsyncTaskLoader<String> 
{ 
       public CustomAsyncLoader(Context context) { 
          super(context); 
          // do some initializations here 
       } 

       @Override
       protected void onForceLoad() {
            // TODO Auto-generated method stub
            super.onForceLoad();
       }

       @Override
        public void deliverResult(String apps) {

       }

       @Override
        protected void onStopLoading() {
            // Attempts to cancel the current load task if possible
            cancelLoad();
        }

       @Override
        public void onCanceled(String apps) {
            super.onCanceled(apps);
        }

        @Override
        public String loadInBackground() {
             String result = ""; 
              // ... 
              // do long running tasks here 
              // ... 
              return result;            
        }
}

Here i will show you the 3 methods overwritted:

@Override
public Loader<String> onCreateLoader(int arg0, Bundle arg1) {
    // TODO Auto-generated method stub
    return new CustomAsyncLoader(root.getContext());
}

@Override
public void onLoadFinished(Loader<String> arg0, String arg1) {
    // TODO Auto-generated method stub
    Toast.makeText(mContext, "onLoadFinish", Toast.LENGTH_LONG).show();
}

@Override
public void onLoaderReset(Loader<String> arg0) {
    // TODO Auto-generated method stub

} 

In the method onResume of my fragment i am calling to init the loader:

getLoaderManager().initLoader(0, null, this).forceLoad();

and the last detail to comment is how the fragment implemented the loader callback:

public class FragmentName extends CustomFragment implements LoaderManager.LoaderCallbacks<String> 

Let see if anybody could help me about how implement it. Thanks in advance.

Upvotes: 0

Views: 566

Answers (1)

michal.z
michal.z

Reputation: 2075

You must call super.deliverResult(apps) in deliverResult method. Otherwise super class of your CustomAsyncLoader won't take care of delivering result to registered listener.

Upvotes: 1

Related Questions