[英]Android anonymous class Loaders leak safe?
In Android development, I've learnt that AsyncTask defined as a non-static nested class is not ideal because it may cause memory leaks when the Activity that started the task, dies before the task has finished processing. 在Android开发中,我了解到将AsyncTask定义为非静态嵌套类并不理想,因为当启动任务的Activity在任务完成处理之前死亡时,它可能导致内存泄漏。
So the solution to use Loaders , whose lifecycle is independent to that of the activity. 因此,可以使用生命周期独立于活动周期的Loaders的解决方案。
However, what about in a situation like this where they've defined an anonymous AsyncTaskLoader. 然而,关于在什么情况下这样的 ,他们已经定义了一个匿名AsyncTaskLoader。 It looks to me that this Loader has a reference to its outer activity.
在我看来,此加载程序对其外部活动具有引用。
(1) Does this not cause a memory leak, where the starting activity is unable to be garbage collected? (1)这不会导致内存泄漏吗?在这种情况下,启动活动无法被垃圾回收?
Furthermore, the Loader's onStartLoading() method holds a reference to the outer activity's member mLoadingIndicator. 此外,加载程序的onStartLoading()方法保留对外部活动的成员mLoadingIndicator的引用。
(2) If onCreateLoader is only called the first time the application launches, will this loader forever latch on to that first activity's mLoadingIndicator, ignoring the view from the new activity? (2)如果仅在应用程序首次启动时调用onCreateLoader,则此加载程序是否会永久性地锁定到该第一个活动的mLoadingIndicator,而忽略新活动中的视图? (For example after configuration change)
(例如,更改配置后)
However, what about in a situation like this where they've defined an anonymous AsyncTaskLoader.
但是,在这样的情况下,他们定义了匿名的AsyncTaskLoader,该怎么办? It looks to me that this Loader has a reference to its outer activity.
在我看来,此加载程序对其外部活动具有引用。
Yes, it has. 是的,它有。
(1) Does this not cause a memory leak, where the starting activity is unable to be garbage collected?
(1)这不会导致内存泄漏吗?在这种情况下,启动活动无法被垃圾回收?
Yes, it does. 是的,它确实。 If this
Loader
runs indefinitely and longer than the encompassing Activity
, it may prevent the garbage collection of the context. 如果此
Loader
无限期地运行并且比包含的Activity
更长,则可能会阻止上下文的垃圾回收。
(2) If onCreateLoader is only called the first time the application launches, will this loader will forever latch on to that first activity's mLoadingIndicator, ignoring the view from the new activity?
(2)如果仅在应用程序首次启动时调用onCreateLoader,那么此加载器将永远锁定在该第一个活动的mLoadingIndicator上,而忽略新活动中的视图吗? (For example after configuration change)
(例如,更改配置后)
onCreateLoader
doesn't latch on to the view mLoadingIndicator
is referencing, but it only calls one of its methods. onCreateLoader
不会锁定到mLoadingIndicator
所引用的视图,但是它仅调用其方法之一。 What really matters is the object mLoadingIndicator
refers to at the time onCreateLoader
is invoked. 真正重要的是在调用
onCreateLoader
mLoadingIndicator
引用的对象。
Actually the loader latches on to the outer activity. 实际上,加载程序会锁定外部活动。 If a configuration change created a new loading indicator view and only then
onCreateLoader
is invoked, the method will use the new view. 如果配置更改创建了新的加载指示器视图,然后才调用
onCreateLoader
,则该方法将使用新视图。
An AsyncTaskLoader
can refer to an Activity
without causing memory leak by wrapping it in a WeakReference
. AsyncTaskLoader
可以通过将Activity
包装在WeakReference
Activity
而不会导致内存泄漏。
public class MyAsyncTaskLoader extends AsyncTaskLoader<String> {
private final WeakReference<Activity> mActivity;
public MyAsyncTaskLoader(Activity activity) {
super(activity);
mActivity = new WeakReference<>(activity);
}
public doYourThing() {
Activity activity = mActivity.get();
// if activity is destroyed and garbage collected,
// it will be null
if (activity != null) {
activity.getYourView().setWhatever();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.