Building responsive apps with CursorLoader
CursorLoader
is a specialized subclass of AsyncTaskLoader
that uses its lifecycle methods to correctly manage the resources associated with a database Cursor
.
A database Cursor
is a little like an Iterator
, in that it allows you to scroll through a dataset without having to worry where exactly the dataset is coming from or what data structure it is a part of.
We're going to use CursorLoader
to query the MediaStore for a list of all images on the device. Because CursorLoader
is already implemented to correctly handle all of the details of working with a Cursor
, we don't need to subclass it. We can simply instantiate it, passing in the information it needs in order to open the Cursor
it should manage for us. We can do this in the onCreateLoader
callback:
@Override public CursorLoader onCreateLoader(int id, Bundle bundle) { return new CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{ MediaStore...