【问题标题】:What is the lifecycle of Loaders?Loaders 的生命周期是什么?
【发布时间】:2015-03-19 15:30:56
【问题描述】:

背景

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

AsyncTask相反,即使由于方向更改而重新创建活动,使用加载器也可以继续执行后台任务。

问题

我想问一下Loaders的生命周期:

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

【问题讨论】:

    标签: android android-asynctask loader asynctaskloader


    【解决方案1】:
    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 lifecycle 与活动/片段生命周期相关联,因此可以安全地假设垃圾收集几乎同时发生。看看#8 加载器的生命周期。可能会给你一些想法。

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

    不,据我所知,装载机没有onPause()

    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.
    

    当然片段可以有加载器。只需在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?
    

    据我所知:

    • 创建活动/片段时,加载程序启动 -> onStartLoading()

    • 当活动变得不可见或片段被分离时,加载器停止 -> onStopLoading()

    • 重新创建活动或片段时没有回调。 LoaderManager 将结果存储在本地缓存中。

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

    我希望这会有所帮助。我可能对某些答案有点偏离。我自己也在不断地学习新事物。 干杯。

    【讨论】:

    • 4.但这意味着加载器实际上是由(并绑定到)活动而不是片段创建的,这意味着 5 需要一个答案:)。 6、对于AsyncTask,可以调用cancel(true),除了设置一个标志外,还会中断AsyncTask的线程。加载完成后会调用 onCancelled 吗?如果是这样,那不是和“放弃”差不多吗? 7. 为什么? 8. 我认为当活动本身(不是片段)被销毁并且不是因为方向变化时才会被销毁。其他配置更改呢?
    • 另外,2. 这只是一个猜测。我认为其中一种状态表明了这一点,因为有“onStopLoading”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 2018-04-15
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多