同一個Loader對象傳入不同參數時,从数据库中查询的結果每次都一樣

發現問題:

LoaderManager().initLoader()方法調用時會根據第一個參數ID去判斷是否已經存在一個Loader加載器,如果存在則複

,不存在則建一個新的加載器。由於我第一次已經建立了一個Loader,所以第二次再調用这个方法的時候系统会认为

经存在这样一个加载器,不會重新傳入新參數构造新的Loader而是直接用之前的Loader對象去加載,結果也就和第一

次的结果一樣,

解決辦法:

當需要向Loader傳遞新的參數時查詢時,應該使用LoaderManager().restartLoader()方法。重新傳入參數進行查詢。

参考:

Per LoaderManager.initLoader():

Ensures a loader is initialized and active. If the loader doesn't already exist, one is created and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used.

Use restartLoader to restart the loader with new arguments.

http://stackoverflow.com/questions/19099260/android-loader-returning-incorrect-item


原文地址:https://www.cnblogs.com/krislight1105/p/3748324.html