android - Loader 的生命周期是多少?

标签 android android-asynctask loader asynctaskloader

背景

我正在努力通过支持横向模式来改进应用。我经常使用的一件事是 Loaders ,或更具体地说 AsyncTaskLoaders .

使用加载器允许您继续执行后台任务,即使由于方向更改而重新创建 Activity ,而不是 AsyncTask .

问题

想问一下Loader的生命周期:

  1. 他们什么时候进行 GC 处理?我是否必须跟踪它们,如果其中有位图,我是否应该尽快放弃它?他们是否可能仅在 Activity 真正被销毁(而不是因为配置更改)后才进行 GC 处理?
  2. 我注意到它们处于停止状态。这会以某种方式让我暂停它们吗?
  3. 如果 #2 为真,我将如何实现一个可以在自身某些点暂停的加载器?
  4. fragment 也可以有加载器吗?正如我所注意到的,它仅用于 Activity 。
  5. 如果#4 为假,在替换容器中的 fragment 的抽屉导航设计模式中,推荐使用加载器的方式是什么?
  6. AsyncTaskLoader 可以像 AsyncTask(或线程)一样被中断吗?我查看了它的代码和 API,但找不到。我也尝试过寻找解决方法,但没有成功。
  7. 如果#6 为假,是否有替代方案?例如,如果我知道加载程序不需要加载某些东西,我可以立即停止它。我能想到的一种方法是设置一个标志(也许是 AtomicBoolean,以防万一),告诉它停止,有时在内部检查这个值。问题是我什至需要在它使用的函数内部添加它,而更简单的方法是调用“Thread.sleep(0)”或类似的东西。
  8. 是否有关于 Loader 生命周期的解释?
  9. AsyncTaskLoaders 是否同时协同工作,或者它们是否类似于 AsyncTask 的默认当前行为,后者仅在单个线程上运行?

最佳答案

1.When do they get GC-ed ? Do I have to keep track of them, and if one has a bitmap inside, should I abandon it as soon as possible? Do they perhaps get GC-ed only after the activity is really destroyed (and not because of configuration changes) ?

由于 loader 生命周期 与 activity/fragment 生命周期相关联,因此可以安全地假设垃圾回收几乎同时发生。查看 #8 以了解加载程序的生命周期。可能会给你一些想法。

2.I've noticed they have states of being stopped. Does this somehow allow me to pause them?

没有,据我所知,加载器没有一个onPause() per say。

3.If #2 is true, How would I implement a loader that can be paused on some points of itself?

这个我真的没有答案。我自己想知道解决方案。

4.Can fragments also have Loaders? As I've noticed, it's only for activities.

当然 fragment 可以有加载器。只需在 onActivityCreated() 方法中初始化 loaderManager

5.if #4 is false, what is the recommended way to use loaders in the design pattern of navigation-drawer that replaces fragments in the container?

4 是真的。所以我想这个问题是无关紧要的。

6.Can AsyncTaskLoader be interrupted like AsyncTask (or threads)? I've looked at its code and at the API, but I can't find it. I've also tried to find a workaround, but I didn't succeed.

我不确定您所说的中断加载程序是什么意思。但是,如果您的意思是使用类似于 isCancelled() 方法的方法,那么 AsyncTaskLoader 上有一个名为 cancelLoad() 的方法。完整的流程就像 cancelLoad()->cancel()->onCancelled() 我认为。

7.If #6 is false, is there an alternative? For example, if I know that the loader doesn't need to load something, I could just stop it right away. One way I can think of is to set a flag (maybe AtomicBoolean, just in case) that will tell it to stop, and check this value sometimes within. Problem is that I will need to add it even inside functions that it uses, while an easier way would be to call "Thread.sleep(0)" or something like that.

又不相关了?

9.Do AsyncTaskLoaders work together, at the same time, or are they like the default, current behavior of AsyncTask, which runs only on a single thread ?

在单线程上运行。

8.Is there somewhere an explanation of the lifecycle of Loaders?

据我所知:

  • 创建 Activity/fragment 时加载器启动 -> onStartLoading()

  • 当 Activity 变得不可见或 fragment 分离时,加载器停止 -> onStopLoading()

  • 重新创建 Activity 或 fragment 时没有回调。 LoaderManager 将结果存储在本地缓存中。

  • 当 Activity/fragment 被销毁时 -> restartLoader()destroyLoader() 被调用并且加载器重置。

希望对您有所帮助。我可能对某些答案有点不满意。我自己也在不断学习新事物。 干杯。

关于android - Loader 的生命周期是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29148721/

相关文章:

java - 上传前文件上传进度跳到 100%

android - 获取异步任务中的调用 fragment

java - 无法从 AsyncTask 更新 UI

c - (&) 的地址给出编译器生成的地址或加载器生成的地址?

android - 在 Android 设备中为 SSO 安装 SSL 证书

android - 如何检查 Android Unity3d 的互联网连接类型

JavaFX fxml 加载器无法正常工作

sql - 如何将 CSV 数据加载到列数多于 csv 文件的 Oracle 表中?

ConstraintLayout中的Android RecyclerView不滚动

android - Phonegap - 下拉菜单不可点击(Android 和 iOS)