Why AsyncTask can't implement methods except doInBackground?

723 Views Asked by At

In Android Studio, I've tried to implement the methods interface of AsyncTask, but it only shows method doInBackgound(). I've tried to place the cursor on the AsyncTask and then press Alt+Enter, but it only offers method doInBackgound().

screenshot

import android.os.AsyncTask;
import java.net.URL;
public class MyTask extends AsyncTask<URL, Integer, Long> {
    @Override
    protected Long doInBackground(URL... urls) {
        return null;
    }
}
3

There are 3 best solutions below

1
Patrick W On BEST ANSWER

The reason you're seeing only the AsyncTask::doInBackground(URL...) is because the method is not implemented in the abstract class AsyncTask.

However, the others have default implementations and so you can only override them. If you need IntelliJ or Android Studio to suggest the others, you need to do CTRL + O. This will suggest other methods you can override from the parent classes.

0
Anjana On

Try pressing Ctrl + O for Override methods. See whether OnPostExecute() can be implemented from there.

Important: OnPostExecute() needs an argument which return from doInBackground(). If the argument is not provided and the method signatures do not match then override annotation may not recognize the method.

0
Siddarth Jain On
    public class MyTask extends AsyncTask<URL, Integer, Long> {
    @Override
    protected Long doInBackground(URL... urls) {
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Long aLong) {
        super.onPostExecute(aLong);
    }
    }