Overriding pre/post execute in AsyncTask and calling super.onPre/PostExecute

android, android-asynctask, java

Solution

No, you don't need to call `super`. Here's the source.

As you can see, the default implementation does nothing.

/**
* Runs on the UI thread before {@link #doInBackground}.
*
* @see #onPostExecute
* @see #doInBackground
*/
protected void onPreExecute() {
}

/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param result The result of the operation computed by {@link #doInBackground}.
*
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@SuppressWarnings({"UnusedDeclaration"})
protected void onPostExecute(Result result) {
}

Problem

Is it mandatory to call super.onPreExecute when overriding onPreExecute in an AsyncTask? What does AsyncTask.onPreExecute and other methods actually do? Same question for onPostExecute and onCancelled ``` public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> { @Override protected void onCancelled(Boolean result) { super.onCancelled(result); //<-DO I HAVE TO? //My onCancelled code below } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); //<-DO I HAVE TO? //My onPostExecute code below } @Override protected void onPreExecute() { super.onPreExecute(); //<-DO I HAVE TO? //My onPreExecute code below } @Override protected Boolean doInBackground(Void... params) { return null; } ```

Original source