如何从 AsyncTask 中的 onPostExecute 发回数据? [英] how do i send data back from onPostExecute in an AsyncTask?

查看:24
本文介绍了如何从 AsyncTask 中的 onPostExecute 发回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题和这个一样Activity的实例变量不被设置在 AsyncTask 的 onPostExecute 或如何从 AsyncTask 返回数据到主 UI 线程但我想将数据发送回同一个调用活动.Intent 的不 startActivity 总是重新启动 Activity

my issue is the same as this Instance variable of Activity not being set in onPostExecute of AsyncTask or how to return data from AsyncTask to main UI thread but i want to send the data back to the same calling activity. Doesnt startActivity for intents always restart the activity

推荐答案

On 选项是使用侦听器,您可以在其中创建活动实现的界面,例如:

On option is to use listeners, where you create an interface that your activity implents, something like:

public interface AsyncListener {
    public void doStuff( MyObject obj );
}

这样,如果您要继承 AsyncTask,则很容易添加此侦听器,然后在 onPostExecute() 中,您可以执行以下操作:

That way, if you're subclassing AsyncTask, it is easy to add this listener, then in onPostExecute(), you could do something like:

protected void onPostExecute( MyObject obj ) {
   asyncListener.doStuff(obj);
}

这篇关于如何从 AsyncTask 中的 onPostExecute 发回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆