6

I'm trying to pass params into a AsyncTaskLoader. How do I do that?

Currently, I'm putting what I need to pass in in a separate static class. Anyway around this?

public class NewsAsyncTaskLoader extends AsyncTaskLoader<List<Content>> {

    private static final DbHelper dbHelper = DbHelperFactory.getDbHelper();

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

    @Override
    public List<Content> loadInBackground() {
        List<Content> contents = DbHelper.getStream(FeedSections.getInstance().getCurrentSection());

        feed.setContents(contents);

        return feed;
    }
}
Bryan
  • 3,220
  • 3
  • 26
  • 31

1 Answers1

18

Pass additional parameters into your constructor:

public FeedAsyncTaskLoader(Context context, String moreInfo) {
    super(context);
    // Do something with moreInfo
}
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    I tried that. It works for the first time. So what happens when you want to refresh the data for a 2nd time with a different param? Will does the onCreateLoader gets called again? – Bryan Sep 12 '13 at 23:35
  • 2
    Use [LoaderManager](http://developer.android.com/reference/android/app/LoaderManager.html)'s `restartLoader` method - your `onCreateLoader` will be called each time and give you an opportunity to pass in new parameters. – ianhanniballake Sep 13 '13 at 01:28