Android: LoaderCallbacks.OnLoadFinished called twice

前端 未结 10 2291
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 11:05

I noticed strange situation using Android Loaders and Fragments. When I invoke LoaderManager.initLoader() after orientation change onLoadFinished is not called (although doc

10条回答
  •  爱一瞬间的悲伤
    2020-12-04 11:28

    This problem manifested itself for me with a CursorLoader returning a Cursor that was already closed:

    android.database.StaleDataException: Attempted to access a cursor after it has been closed.
    

    I'd guess this is a bug or an oversight. While moving initLoader() into onResume may work, what I was able to do was remove the Loader when I'm done with it:

    To start the loader (in my onCreate):

      getLoaderManager().initLoader(MUSIC_LOADER_ID, null, this);
    

    Then after I'm done with it (basically at the end of onLoadFinished)

      getLoaderManager().destroyLoader(MUSIC_LOADER_ID);
    

    This seems to behave as expected, no extra calls.

提交回复
热议问题