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> {

    /**
     * `doInBackground` is run on a separate, background thread
     * (not on the main/ui thread). DO NOT try to update the ui
     * from here.
     */
    @Override
    protected Void doInBackground(Void... params) {
        deleteSelectedGalleryItems();
        return null;
    }

    /**
     * `onPostExecute` is run after `doInBackground`, and it's
     * run on the main/ui thread, so you it's safe to update ui
     * components from it. (this is the correct way to update ui
     * components.)
     */
    @Override
    protected void onPostExecute(Void param) {
        cleanupUiAfterCancelOrDelete();
        galleryItemAdapter.notifyDataSetChanged();
    }
}

I call this AsyncTask like this:

private void handleDeleteSelectedImagesProcess() {
    new DeleteImagesTask().execute();
}

For the purposes of this short tutorial it doesn’t matter what that code does; I just wanted to show how to implement an AsyncTask with Void/null parameters, i.e., the class and method signatures. This is one of those (weird) cases where I didn’t want/need to pass any data to/from the AsyncTask, I just needed to run “something” that didn’t require any input parameters, and I didn’t need any output parameters (results) back from it.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.