从 AsyncTask 内的活动访问实例变量
我有一个 AsyncTask (在一个单独的文件中),它是在 活动。当我实例化 AsyncTask 时,我将活动作为 参数。我如何从以下位置访问活动的实例变量 AsyncTask 的 onPostExecute 方法?
谢谢!
I have an AsyncTask (in a separate file) which is invoked on an
activity. When I instantiate the AsyncTask, I send the activity as a
param. How can I access the acitivity's instance variables from the
onPostExecute method of the AsyncTask?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 Activity 或 Context 传递给不是 Activity 内部(非静态)类的 AsyncTask 时必须小心 - 这是因为在这种情况下,生命周期
Activity
/Context
和AsyncTask
是不同的,并且如果您持有Activity
/上下文
时间比你应该的时间长,你会导致记忆泄漏。您不应在
AsyncTask
中保留 Activity 或活动上下文本身,而应保留对 Activity 的WeakReference
。这将确保垃圾收集器 (GC) 在需要时可以回收与 Activity 关联的内存。传递
Context
时,请尽可能尝试使用ApplicationContext
,因为这是寿命最长的上下文。You must be careful when passing an Activity or
Context
to anAsyncTask
that is not an inner (non-static) class of an Activity - this is because in this case the lifetime of theActivity
/Context
and theAsyncTask
are different, and if you hold on to anActivity
/Context
for longer than you should you will cause memory leaks.Rather than holding onto the Activity or activity context itself in your
AsyncTask
you should keep aWeakReference
to the Activity. This will ensure that the memory associated with the Activity can be reclaimed by the garbage collector (GC) when needed.When passing a
Context
always try to use theApplicationContext
where possible as this is the longest-lived context.与程序员 Bruce 的答案类似,但不是通过 AsyncTask 本身将 Activity 作为参数传递,只需添加一个构造函数来获取父 Activity。我自己的代码示例...
当您在 Activity 中创建它时,只需执行以下操作...
编辑:在回复您的评论时,请确保将 mLogin 声明为
public
然后使用...如果这不起作用,请尝试...
Similar to Programmer Bruce's answer but instead of passing the Activity as a Param through the AsyncTask itself, simply add a constructor to take the parent Activity. Example from my own code...
When you create it in your Activity just do this...
EDIT: In reply to your comment, make sure mLogin is declared as
public
then use...If that doesn't work, try...
您可以从
AsyncTask
进行扩展,然后您可以传入任意数量的任意参数,无需传递对整个 Activity 的引用。您可以在 doInBackground 中引用:
You can extend from
AsyncTask<Object, x x>
, and you can then pass in any number of arbitrary parameters you like, no need to pass around reference to the whole Activity.Which you can reference in your doInBackground: