gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:56 33 4
gpt4 key购买 nike

在 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com