如何将 asynctask onpostexecute 方法的结果传递给父 Activity android [英] how to pass the result of asynctask onpostexecute method into the parent activity android
问题描述
我正在开发一个应用程序,在该应用程序中,我需要将 asynctask 的 onPostExecute 方法的结果值发送到前一个活动,即调用 aync 任务的活动.请放置一些代码.任何帮助表示赞赏
I am developing an application in which i need to send the value of the asynctask's onPostExecute method's result in to the previous activity , ie the activity in which the aync task is being called.pls put some codes. Anyhelp is appreciated
推荐答案
两种方式:
- 在父 Activity 中将扩展 AsyncTask 的类声明为私有类
- 将处理程序或活动本身作为扩展 AsyncTask 的类的参数
如果我是你,我会选择第一个选项.
看看DOCS:
If I were you, I'd follow the first option.
Look at DOCS:
class MyActivitySubclass extends Activity {
function runOnPostExecute(){
// whatever
}
private class MyTask extends AsyncTask<Void, Void, Void> {
void doInBackground(Void... params){
// do your background stuff
}
void onPostExecute(Void... result){
runOnPostExecute();
}
}
}
<小时>
注意 1
onPostExecute
函数体中的代码已经在 Activity 线程上运行,你应该提到 this
关键字导致 MyTask.this
而不是 MyActivitySubclass.this
Code placed in body of function onPostExecute
is already run on Activity thread, you should just mention that this
keywords leads to MyTask.this
and not MyActivitySubclass.this
这篇关于如何将 asynctask onpostexecute 方法的结果传递给父 Activity android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!