全球装载机(LoaderManager)再用于多个活动/片段 [英] Global Loader (LoaderManager) for reuse in multiple Activities / Fragments
问题描述
我想实现:
我有两个不同的片段。我想他们都表现出相同的数据以两种形式(以列表和地图上的)。我希望他们共享一个装载机( AsyncTaskLoader
尤其是)。一切工作正常,但不重复使用的装载机。另一个被创建,数据被加载两次。
我做的:
在片段
的I使用 LoaderManager LM = getActivity()getSupportLoaderManager();
在他们两个人我实施 LoaderCallbacks< ArrayList的<项目>>
和所需的方法。
在这两个我用 lm.initLoader(0,指定参数时,这一点);
但是,当我输出 lm.toString()
看来,这是两个不同的装载机。并且,数据被下载两次
如何从不同的活动/片段比它是在开始重新连接到相同的装载机?
由于上下文被附加到Loader反正每个的onCreate()
,例如,它应该是可能的在配置更改。
如何从一个不同的活动/片段比它是在开始重新连接到相同的装载机?
您的不应该再利用装载机
正在由 LoaderManager
实例管理小号在多个活动
和片段
秒。
在 LoaderManager
将启动/停止那些装载机
s的关于活动
/ 片段
的生命周期,所以没有办法保证那些装载机
取值会存在一次您在另一个活动
。
从文档:
LoaderManager.LoaderCallbacks是一个回调接口,它允许 客户端与LoaderManager交互。
装载机,特别CursorLoader,预计将保留它们的数据 之后被停止。这使得应用程序的数据保持 横跨活性或片段的的onStop()和ONSTART()方法,使 当用户返回到一个应用程序,它们不必等待 数据重新加载。您可以使用LoaderManager.LoaderCallbacks方法 当知道什么时候创建一个新的Loader,并告诉应用程序 当是时候停止使用加载器的数据。
在换句话说,它常常是您的装载机
取值将特定于某些活动(或片段)的情况。当你有你的活动
实施 LoaderManager.LoaderCallbacks
界面,你的活动是给定类型 LoaderManager .LoaderCallbacks
。每次调用 initLoader(INT ID,捆绑的args,LoaderCallbacks< D>回调)
,该LoaderManager创建或者重用装载机
特定于所述 LoaderManager.LoaderCallbacks
接口(在此情况下,是你的活动的一个实例)的一些实例。这在本质上与装载机结合的活动,它的回调方法将被称为加载状态的变化。
话虽这么说,除非你能找到一种方法,让您的两个独立Activitys共享相同的回调方法,我怀疑有一个干净的方式来做到这一点(即有一个活动,一个片段共享相同的回调听起来像将是棘手的,如果不是不可能的)。我不会担心这虽然太多。在所有的样本code我所见过的,我从来没有看过两Activitys和/或片段共享相同的回调方法。此外,因为活动
和片段
,则两者应该被设计为重复使用,共享装载机
S在这种方式只是似乎并不喜欢的事,会受到鼓励。
What I would like to achieve:
I have two different fragments. I would like them both to show the same data in two forms (in a list and on a map). I would like them to share one Loader (AsyncTaskLoader
in particular). Everything works fine, but the Loader isn't re-used. Another one is created and the data is loaded twice.
What I do:
In the Fragment
s I use LoaderManager lm = getActivity().getSupportLoaderManager();
In both of them I implement LoaderCallbacks<ArrayList<Item>>
and the required methods.
In both I use lm.initLoader(0, args, this);
.
But when I output the lm.toString()
it appears that these are two different Loaders. And the data is downloaded twice.
How to re-connect to the same Loader from a different Activity/Fragment than the one it was started in?
It should be possible since the context is attached to the Loader anyway on every onCreate()
, e.g. on configuration change.
How to re-connect to the same Loader from a different Activity/Fragment than the one it was started in?
You should not reuse Loader
s that are being managed by a LoaderManager
instance across multiple Activity
s and Fragment
s.
The LoaderManager
will start/stop those Loader
s with respect to the Activity
/Fragment
lifecycle, so there is no way of guaranteeing that those Loader
s will exist once you are in another Activity
.
From the documentation:
LoaderManager.LoaderCallbacks is a callback interface that lets a client interact with the LoaderManager.
Loaders, in particular CursorLoader, are expected to retain their data after being stopped. This allows applications to keep their data across the activity or fragment's onStop() and onStart() methods, so that when users return to an application, they don't have to wait for the data to reload. You use the LoaderManager.LoaderCallbacks methods when to know when to create a new loader, and to tell the application when it is time to stop using a loader's data.
In other words, it is often the case that your Loader
s will be specific to some Activity (or Fragment). When you have your Activity
implement the LoaderManager.LoaderCallbacks
interface, your Activity is given type LoaderManager.LoaderCallbacks
. Each time you call initLoader(int ID, Bundle args, LoaderCallbacks<D> callback)
, the LoaderManager either creates or reuses a Loader
that is specific to some instance of the LoaderManager.LoaderCallbacks
interface (which in this case is an instance of your Activity). This essentially binds your Activity with a Loader, and its callback methods will be called as the loader state changes.
That being said, unless you can find a way to have your two separate Activitys share the same callback methods, I doubt there is a clean way to do this (i.e. having an Activity and a Fragment share the same callbacks sounds like it would be tricky, if not impossible). I wouldn't worry about it too much though. In all of the sample code I have ever seen, I've never seen two Activitys and/or Fragments share the same callback methods. Further, given that Activity
s and Fragment
s are both supposed to be designed for reuse, sharing Loader
s in this way just doesn't seem like something that would be encouraged.
这篇关于全球装载机(LoaderManager)再用于多个活动/片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!