Android Loader edit

Basic AsyncTaskLoader

AsyncTaskLoader is an abstract Loader that provides an AsyncTask to do the work.

Here some basic implementation:

final class BasicLoader extends AsyncTaskLoader<String> {

    public BasicLoader(Context context) {

    public String loadInBackground() {
        // Some work, e.g. load something from internet
        return "OK";

    public void deliverResult(String data) {
        if (isStarted()) {
            // Deliver result if loader is currently started

    protected void onStartLoading() {
        // Start loading

    protected void onStopLoading() {

    protected void onReset() {

        // Ensure the loader is stopped

Typically Loader is initialized within the activity’s onCreate() method, or within the fragment’s onActivityCreated(). Also usually activity or fragment implements LoaderManager.LoaderCallbacks interface:

public class MainActivity extends Activity implements LoaderManager.LoaderCallbacks<String> {

    // Unique id for loader
    private static final int LDR_BASIC_ID = 1;

    protected void onCreate(Bundle savedInstanceState) {

        // Initialize loader; Some data can be passed as second param instead of Bundle.Empty
        getLoaderManager().initLoader(LDR_BASIC_ID, Bundle.EMPTY, this);

    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new BasicLoader(this);

    public void onLoadFinished(Loader<String> loader, String data) {
        Toast.makeText(this, data, Toast.LENGTH_LONG).show();

    public void onLoaderReset(Loader<String> loader) {

In this example, when loader completed, toast with result will be shown.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close