starting a loader:
The LoaderManager
manages one or more Loader
instances within an Activity
or Fragment
. There is only one LoaderManager
per activity or fragment.
你在Activity中oncreate()方法中,或是在fragment中的 onActivityCreated()方法中实现
代码如下:
getLoaderManager().initLoader(0, null, this);其中第一个参数:loader的唯一标识符,第二个是一个可选参数提供给Loader的构造函数,第三个参数:对LoaderManager.LoaderCallbacks的实现,调用loaderManager报告loader事件。
LoaderManager会自动的管理Loader,很少直接对loader进行操作,通常是使用LoaderManager.LoaderCallbacks方法来处理loading过程。
Restarting a Loader:
当你想抛弃你的数据的时候,需要调用restartLoader().
例如:
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;
}
LoaderManager.LoaderCallbacks
包括如下的方法:
onCreateLoader():返回一个给定ID号的Loader;
onLoaderFinished():当先前的Loader完成了load的时候被调用
onLoaderReset():数据难以获取的时候,重启先前的Loader