java - 在 AsyncTask 中覆盖执行前/后执行并调用 super.onPre/PostExecute

标签 java android android-asynctask

在 AsyncTask 中覆盖 onPreExecute 时是否必须调用 super.onPreExecute? AsyncTask.onPreExecute 和其他方法实际上做了什么? onPostExecute 和 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;
}

最佳答案

不,你不需要调用super。这是 source .

如您所见,默认实现不执行任何操作。

/**
* 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) {
}

关于java - 在 AsyncTask 中覆盖执行前/后执行并调用 super.onPre/PostExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21703774/

相关文章:

java - 错误处理的 Spring MVC Rest 服务 Controller 正确吗?

Java Swing GUI - 界面困惑 - 字段未显示

java - jTDS - 运行 Maven 构建的项目时没有合适的驱动程序异常

android - 如何声明一个对所有模块的 build.gradle 文件可见的常量?

Android 通用图像加载器和几个图像的 url

java - 如何查看 JTable 中的列标签?

android - 单击事件不适用于 android 中的线性布局

android - 在 ViewModel 类中使用新线程

android - AsyncTask 的 doInBackground(Params...params)

java - Android:不确定为什么我的方法没有返回 ArrayList<HashMap<String, String>>