Skip to content Skip to sidebar Skip to footer

Onloadfinished Not Called After Coming Back From A Home Button Press

When using a custom AsyncTaskLoader to download data from a web service, if I press the HOME button in the middle of the loading process and then enter the app again, the onLoadFin

Solution 1:

After looking at Issue 14944 (http://code.google.com/p/android/issues/detail?id=14944), I solved the issue by overriding onStartLoading() in my custom AsyncTaskLoader and call forceLoad().

An even better solution is to create a custom parent AsyncTaskLoader that looks like this (taken from a suggestion by alexvem from the link above):

publicabstractclassAsyncLoader<D> extendsAsyncTaskLoader<D> {

    private D data;

    publicAsyncLoader(Context context) {
        super(context);
    }

    @OverridepublicvoiddeliverResult(D data) {
        if (isReset()) {
            // An async query came in while the loader is stoppedreturn;
        }

        this.data = data;

        super.deliverResult(data);
    }


    @OverrideprotectedvoidonStartLoading() {
        if (data != null) {
            deliverResult(data);
        }

        if (takeContentChanged() || data == null) {
            forceLoad();
        }
    }

    @OverrideprotectedvoidonStopLoading() {
         // Attempt to cancel the current load task if possible.cancelLoad();
    }

    @OverrideprotectedvoidonReset() {
        super.onReset();

        // Ensure the loader is stoppedonStopLoading();

        data = null;
    }
}

Post a Comment for "Onloadfinished Not Called After Coming Back From A Home Button Press"