作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 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/
我是一名优秀的程序员,十分优秀!