Will LoaderManager.restartLoader() always result in a call to onCreateLoader()?

前端 未结 1 1595
面向向阳花
面向向阳花 2021-02-04 06:22

LoaderManager has this method restartLoader():

public abstract Loader restartLoader (int id, Bundle args, LoaderCallbacks

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 07:09

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

    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 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 onCreateLoader(int id, Bundle args)
    {
        if (id == 0)
            //return a Loader for id 0
        else if (id == 1)
            //return a Loader for id 1
    }
    

    The Loader Manager passes the resulting loader to onLoadFinished:

    public void onLoadFinished(Loader 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 is first called:

    public void onLoaderReset(Loader 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
    }
    

    ...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.

    0 讨论(0)
提交回复
热议问题