Bobbake4 Bobbake4 - 5 months ago 20
Java Question

Android AsyncTaskLoader doesn't start loadInBackground?

I am trying to implement a loader example on Android but can't get it to start the loader. I am using the following code. It will hit the "Create Loader" but it will never reach the "Loading started" log message. Am I missing a call that I need?

Activity:

public class TestingZoneActivity extends ListActivity implements LoaderCallbacks<ArrayList<Content>>{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

@Override
public Loader<ArrayList<Content>> onCreateLoader(int id, Bundle args) {
Log.e("TEST", "Create Loader");
return new ImageLoader(this);
}

@Override
public void onLoadFinished(Loader<ArrayList<Content>> loader, ArrayList<Content> data) {
setListAdapter(new ImageAdapter(this, data));
}

@Override
public void onLoaderReset(Loader<ArrayList<Content>> loader) {
setListAdapter(null);
}
}


Loader:

public class ImageLoader extends AsyncTaskLoader<ArrayList<Content>> {

public ImageLoader(Context context) {
super(context);
}

@Override
public ArrayList<Content> loadInBackground() {
Log.e("TEST", "Loading started");
}

}

Answer

I had the same problem using the compatibility library. I solved it by calling forceLoad

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

Obviously the documentation on AsyncLoader is lacking and this problem also exists on HoneyComb. More information can be found here

The official example of AsyncTaskLoader is also calling forceLoad() so its not a bug, but i still think that that behavior is not very intuitive.

Comments