在使用initLoader()方法时,如果指定ID的装载器存在,就使用这个既存的装载器,否则会创建一个新的。但是有些时候你会想要废弃旧的数据并重启装载器。
你可以使用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;
}
注:本人转载系个人觉得翻译的很好,值得收藏,且自己回头看着方便。
如有兴趣请访问作者官方博客http://blog.youkuaiyun.com/FireOfStar
本文介绍了如何使用restartLoader()方法来更新搜索过滤器,并重新加载数据。当用户更改搜索条件时,此方法可以确保装载器获取最新的数据。
1591

被折叠的 条评论
为什么被折叠?



