asynctask

My Android AsyncTask docs and examples (parameters, callbacks, executing, canceling)

I’ve currently written this document as a “note to self” about how the Android AsyncTask works. It’s currently incomplete, but if you want to know how an AsyncTask works, most of the answers are generally here. I provide documentation for most aspects of the AsyncTask, though my coverage of (a) updating progress/status and (b) canceling an AsyncTask is a little weak atm.

Source code for an Android AsyncTask (REST client) example

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

Android Room database persistence library examples

As a note to self, here are some Android Room database persistence library examples:

Those tutorials don’t show how to properly use Room database access methods, so they’ll lead to Android “Application Not Responding” (ANR) errors. Therefore, here are some related Google/Android docs:

Finally, here’s my own Android AsyncTask REST example, which also shows how to use an AsyncTask.

An example of Android StrictMode output (with improper database access)

I was just working with an example of how to use Android’s new Room Persistence Library, and the example I was working with ran some of its code on the main Android thread, also known as its “UI thread.” I knew this was bad, but I wanted to start with someone’s example, and then figure out a good way to get the Room method calls to run on a background thread, such as using an AsyncTask. (The Android docs don’t specify a “best practice” for this atm.)

Android AsyncTask - Using "Void, Void, Void" parameters (code signature)

As a quick note to self, this is how I just created an Android AsyncTask with “Void, Void, Void” parameters:

private class DeleteImagesTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        deleteSelectedGalleryItems();
        return null;
    }
    @Override
    protected void onPostExecute(Void param) {
        cleanupUiAfterCancelOrDelete();
        galleryItemAdapter.notifyDataSetChanged();
    }
}

A simple Android AsyncTask example

This page on the Android developer’s blog shows what is just about the simplest possible AsyncTask you can create, which is exactly what I needed just now. I started testing my Android app with StrictMode enabled this morning, and it was kind enough to remind me that I was doing some file I/O on the main thread, which is a no-no; hence the AsyncTask.

An Android GridView of text quotes (Fragment, layout, and AsyncTask source code)

Before I completely delete this code from my current Android application, I want to make a copy of it here. It was intended to show a series of quotes (text phrases) in a “Grid” (GridView), but (a) I never got it working as desired, and (b) I decided I didn’t want it in my application anyway.

Here’s the source code for the Java controller/fragment class: