gpt4 book ai didi

android - LoaderManager.restartLoader() 是否总是会导致调用 onCreateLoader()?

转载 作者:IT老高 更新时间:2023-10-28 23:40:52 26 4
gpt4 key购买 nike

LoaderManager有这个方法 restartLoader() :

public abstract Loader<D> restartLoader (int id, Bundle args, LoaderCallbacks<D> callback)

Starts a new or restarts an existing Loader in this manager, registers the callbacks to it, and (if the activity/fragment is currently started) starts loading it. If a loader with the same id has previously been started it will automatically be destroyed when the new loader completes its work. The callback will be delivered before the old loader is destroyed.

基于 the dev guide ,我知道确实,调用onCreateLoader总是来自 restartLoader() :

Restarting a Loader

...

To discard your old data, you use restartLoader(). For example, this implementation of SearchView.OnQueryTextListener restarts the loader when the user's query changes. The loader needs to be restarted so that it can use the revised search filter to do a new query:

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是唯一将用户查询信息传递给加载器的地方(在实例化时)。然而,文档说“启动一个新的或重新启动一个现有的加载程序”让我失望。

最佳答案

您的问题的简单答案是肯定的,对 restartLoader() 的调用将再次调用 onCreateLoader()。

您可以并行启动多个加载程序(假设您有两个 SimpleCursorAdapter 要填充),例如:

getLoaderManager().initLoader(0, null, this);  //id = 0
getLoaderManager().initLoader(1, null, this); //id = 1

然后加载器管理器为每个 id 调用 onCreateLoader(返回的加载器然后由加载器管理器异步构建):

public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
if (id == 0)
//return a Loader<Cursor> for id 0
else if (id == 1)
//return a Loader<Cursor> for id 1
}

加载器管理器将生成的加载器传递给 onLoadFinished:

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
if (loader.getId() == 0)
//cursor was returned from onCreateLoader for id 0
//perhaps do swapCursor(cursor) on an adapter using this loader
else if (loader.getId() == 1)
//cursor was returned from onCreateLoader for id 1
//perhaps do swapCursor(cursor) on an adapter using this loader
}

当你随后调用重启加载器时:

getLoaderManager().restartLoader(0, null, this);  //id = 0

...onLoaderReset 首先被调用:

public void onLoaderReset(Loader<Cursor> loader)
{
if (loader.getId() == 0)
//perhaps do swapCursor(null) on an adapter using this loader
else if (loader.getId() == 1)
//perhaps do swapCursor(null) on an adapter using this loader
}

...接着是对 onCreateLoader 的新调用。因此,在这方面,onCreateLoader 既用于启动全新的加载器,也用于重置现有加载器。

关于android - LoaderManager.restartLoader() 是否总是会导致调用 onCreateLoader()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com