【发布时间】:2014-02-10 18:20:01
【问题描述】:
LoaderManager有这个方法restartLoader():
public abstract Loader<D> restartLoader (int id, Bundle args, LoaderCallbacks<D> callback)在这个管理器中启动一个新的或重新启动一个现有的加载器,注册回调,并且(如果活动/片段当前已启动)开始加载它。如果之前已经启动了具有相同 id 的加载器,它将在新加载器完成其工作时自动销毁。回调将在旧加载器被销毁之前传递。
基于the dev guide,我知道确实,对onCreateLoader 的调用总是来自restartLoader():
重新启动加载器
...
要丢弃旧数据,请使用 restartLoader()。例如,当用户查询发生变化时,SearchView.OnQueryTextListener 的这个实现会重新启动加载程序。加载程序需要重新启动,以便它可以使用修改后的搜索过滤器进行新的查询:
public boolean onQueryTextChanged(String newText) {
// Called when the action bar search text has changed. Update
// the search filter, and restart the loader to do a new query
// with this filter.
mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
getLoaderManager().restartLoader(0, null, this);
return true;
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// NOTE: The Loader is instantiated with the user's query
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = Contacts.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
在示例中,onCreateLoader 是唯一将有关用户查询的信息传递给加载器的位置(在实例化时)。然而,文档说“启动一个新的或重新启动一个现有的加载程序”让我失望。
【问题讨论】:
标签: android android-loadermanager android-loader