- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我完全不知道 initLoader
之间的差异和 restartLoader
LoaderManager
的功能:
restartLoader
还创建一个加载器,如果它不存在(“在这个管理器中启动一个新的或重新启动一个现有的加载器”)。 restartLoader
随时拨打
initLoader
?我可以打电话吗
restartLoader
无需调用
initLoader
?打电话安全吗
initLoader
两次刷新数据?我什么时候应该使用两者之一和
为什么 ?
最佳答案
要回答这个问题,您需要深入了解 LoaderManager
代码。
虽然 LoaderManager 本身的文档不够清楚(或者不会有这个问题),但 LoaderManagerImpl(抽象 LoaderManager 的子类)的文档更具启发性。
initLoader
Call to initialize a particular ID with a Loader. If this ID already has a Loader associated with it, it is left unchanged and any previous callbacks replaced with the newly provided ones. If there is not currently a Loader for the ID, a new one is created and started.
This function should generally be used when a component is initializing, to ensure that a Loader it relies on is created. This allows it to re-use an existing Loader's data if there already is one, so that for example when an Activity is re-created after a configuration change it does not need to re-create its loaders.
Call to re-create the Loader associated with a particular ID. If there is currently a Loader associated with this ID, it will be canceled/stopped/destroyed as appropriate. A new Loader with the given arguments will be created and its data delivered to you once available.
[...] After calling this function, any previous Loaders associated with this ID will be considered invalid, and you will receive no further data updates from them.
initLoader
只会替换作为参数传递的回调,但不会取消或停止加载器。对于 CursorLoader
这意味着游标保持打开和 Activity 状态(如果在 initLoader
调用之前是这种情况)。 `restartLoader,另一方面,将取消、停止和销毁加载器(并像游标一样关闭底层数据源)并创建一个新的加载器(如果加载器是一个 CursorLoader)。 LoaderInfo info = mLoaders.get(id);
if (info == null) {
// Loader doesn't already exist -> create new one
info = createAndInstallLoader(id, args, LoaderManager.LoaderCallbacks<Object>)callback);
} else {
// Loader exists -> only replace callbacks
info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback;
}
LoaderInfo info = mLoaders.get(id);
if (info != null) {
LoaderInfo inactive = mInactiveLoaders.get(id);
if (inactive != null) {
// does a lot of stuff to deal with already inactive loaders
} else {
// Keep track of the previous instance of this loader so we can destroy
// it when the new one completes.
info.mLoader.abandon();
mInactiveLoaders.put(id, info);
}
}
info = createAndInstallLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback);
initLoader
仅替换回调 (info.mCallbacks = ...) 而
restartLoader
停用旧加载器(当新加载器完成其工作时,它将被销毁)然后创建一个新加载器。
initLoader
以及何时使用
restartLoader
以及为什么使用这两种方法是有意义的。
initLoader
用于确保有一个初始化的加载器。如果不存在,则创建一个新的,如果已经存在,则重新使用。我们总是使用这种方法,除非我们需要一个新的加载器,因为要运行的查询已经改变(不是底层数据,而是实际查询,如 CursorLoader 的 SQL 语句),在这种情况下,我们将调用
restartLoader
.
initLoader
,如果我们想运行不同的查询,我们使用
restartLoader
.
restartLoader
但这将是低效的。在屏幕旋转之后,或者如果用户离开应用程序并稍后返回到同一个 Activity,我们通常希望显示相同的查询结果,因此
restartLoader
将不必要地重新创建加载器并消除底层(可能很昂贵)的查询结果。
restartLoader
)。如果我们只想显示未结订单,我们需要一个新的查询,我们将使用
restartLoader
返回一个反射(reflect)新查询的新 CursorLoader。
Is there some relation between the two methods?
Does calling
restartLoader
always callinitLoader
?
Can I call
restartLoader
without having to callinitLoader
?
Is it safe to call
initLoader
twice to refresh the data?
initLoader
两次,但不会刷新任何数据。
When should I use one of the two and why?
onLoadFinished()
等等,这很可能会破坏您的应用程序。
initLoader
恢复回调方法(当然,
restartLoader
也是可能的)。
documentation指出:
If at the point of call the caller is in its started state, and the requested loader already exists and has generated its data, then callback
onLoadFinished(Loader, D)
will be called immediately (inside of this function) [...].
initLoader
方向改变后,我们会得到一个
onLoadFinished
立即调用,因为数据已经加载(假设在更改之前是这种情况)。
android:configChanges
的 Activity list 中的标签。这些initLoader/restartLoader
在另一个回调方法中(例如在onActivityCreated(Bundle)
)。为了能够初始化加载器,initLoader(loaderid,
...)
. outState.putIntegerArrayList(loaderIdsKey, loaderIdsArray)
在loaderIdsArray =
savedInstanceState.getIntegerArrayList(loaderIdsKey)
在我们做之前关于java - LoaderManager中initLoader和restartLoader的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445070/
restartLoader(int id, Bundle args, LoaderCallbacks callback)创建一个全新的加载器对象?或者它只是通过重置其内部状态来重用旧的? 感谢您的帮助
我正在开发我的 Android 应用程序,我有一个显示项目列表的 Activity,然后是另一个用于编辑特定项目的 Activity。但是现在我在编辑项目返回时在 restartLoader() 中收
我的应用有一个选择器和一个 ListFragment。在纵向布局中,微调器用作选择器;在景观中使用 ListView 。选择项目时,监听器将根据所选项目刷新 ListFragment 的内容。下面是代
目前,我有一个装载机 @Override public Loader> onCreateLoader(int arg0, Bundle bundle) { return new HomeMen
LoaderManager有这个方法 restartLoader() : public abstract Loader restartLoader (int id, Bundle args, Load
我的 fragment 中有以下自己的接口(interface)实现: @Override public void onReportChanged(Fragment sender, long
我是 Android 的初学者。出于这个原因,我首先设计了带有 Activity 的项目,一切正常。然后为了使用抽屉导航,我将我的 Activity 更改为 fragment 。 我有一个显示新闻列表
我在从 ListFragment 外部调用 restartLoader() 时遇到一点问题。该序列根据点击“添加”按钮打开一个 Activity -> 输入数据 -> 点击保存,这将启动一个 Asyn
当我检索我的联系人时,我试图在我的 listfragment 的 actionBar 中实现一个搜索 View 。 当我尝试使用时我的问题来了 getLoaderManager().restartLo
我正在使用加载器、ActionBarSherlock 和 SqliteCursorLoader 在 fragment 中创建一个 ListView。 ListView 主要显示日期和 GPS 坐标的列
我是一名优秀的程序员,十分优秀!