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"); } }
Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.
To interact with the loader, use one of the LoaderManager callbacks in the activity where the data is needed: Call onCreateLoader() to instantiate and return a new loader for the given ID. Call onLoadFinished() when a previously created loader has finished loading.
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.
Overriding loadInBackground()
is not enough.
Have a look at the AppListLoader
on http://developer.android.com/reference/android/content/AsyncTaskLoader.html .
At least add these two methods to your loader:
@Override protected void onStartLoading() { if (takeContentChanged()) forceLoad(); } @Override protected void onStopLoading() { cancelLoad(); }
and call onContentChanged()
from its constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With