LoaderManager
有一个叫做restartLoader()
的方法:
public abstract Loader<D> restartLoader (int id, Bundle args, LoaderCallbacks<D> callback)
启动新的或重新启动现有的Loader,将回调注册到其中,并(如果当前已启动活动/片段)开始加载它。 如果以前已启动具有相同ID的加载器,则当新加载器完成其工作时,它将自动被销毁。在旧的加载器被销毁之前,将传递回调。
根据开发者指南的说明,我得出的结论是,确实会从restartLoader()
调用onCreateLoader
方法:
重新启动加载器
...
要丢弃旧数据,可以使用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
是唯一一个在实例化时将有关用户查询的信息传递给 Loader 的地方。然而,文档中说“启动新的或重新启动现有的 Loader”,这让我感到困惑。