是否LoaderManager.restartLoader()总是会导致对onCreateLoader()的调用? [英] Will LoaderManager.restartLoader() always result in a call to onCreateLoader()?

查看:453
本文介绍了是否LoaderManager.restartLoader()总是会导致对onCreateLoader()的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LoaderManager 具有此方法 restartLoader()

LoaderManager has this method restartLoader():


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

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

在此启动新的或重新启动现有的Loader管理器,向其注册回调,并且(如果活动/片段当前已启动)开始加载它。如果先前启动了具有相同ID的加载程序,则在新的加载程序完成工作后,它将自动销毁。

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.

基于开发指南,我的想法确实是,调用 onCreateLoader 将始终由 restartLoader()引起:

Based on the dev guide, I get the idea that indeed, a call to onCreateLoader will always result from restartLoader():


重新启动加载程序



...

Restarting a Loader

...

要丢弃旧数据,请使用restartLoader()。例如,当用户查询发生更改时,SearchView.OnQueryTextListener的此实现将重新启动加载程序。加载程序需要重新启动,以便可以使用修改后的搜索过滤器进行新的查询:

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 是有关用户查询的信息(在实例化时)传递给Loader的唯一位置。但是,文档说我启动新的或重新启动现有的加载程序使我失望。

In the example, onCreateLoader is the only place where information about the user's query is passed to the Loader (on instantiation). However the docs throw me off, by saying 'Starts a new or restarts an existing Loader'.

推荐答案

您问题的简单答案是,是的,调用restartReloadLoad()会再次调用onCreateLoader()。

The simple answer to your question is yes, a call to restartLoader() will call onCreateLoader() again.

您可以并行启动多个加载器(例如您有两个SimpleCursorAdapters可以填充),例如:

You can start more than one loader in parallel (say you have two SimpleCursorAdapters to fill), e.g.:

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

onCreateLoader然后由Loader Manager为每个id(即然后由Loader Manager异步构建返回的数据):

onCreateLoader is then called by the Loader Manager for each id (the loader that is returned is then built asynchronously by the Loader Manager):

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:

The Loader Manager passes the resulting loader to 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
}

随后调用重新启动加载器时:

When you subsequently call restart loader:

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

... onLoaderReset首先被调用:

...onLoaderReset is first called:

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既可以用于启动全新的加载程序,也可以在重置现有加载程序时使用。

...followed by a new call to onCreateLoader. So in that respect, onCreateLoader is used both to start a brand new loader and when an existing loader is reset.

这篇关于是否LoaderManager.restartLoader()总是会导致对onCreateLoader()的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆