onCreateLoader not called when orientation changes

后端 未结 4 1563
余生分开走
余生分开走 2021-01-18 03:16

My problem is basically the same as this one:Sometimes don't get onCreateLoader callback after calling initLoader

I have 2 ListFragments that are co

4条回答
  •  终归单人心
    2021-01-18 03:49

    I had the same issue as the OP in that onCreateLoader was not being called after a configuration change. However, that wasn't really the problem (as using getSupportLoaderManager().enableDebugLogging(true); showed that my loaders were being successfully re-used, so there was no need to recreate new ones).

    So I looked into my onLoadFinished(Loader loader, Cursor cursor) method and found, unlike onCreateLoader, it was always called after a configuration change. And the cursor was being populated each time. :-)

    The problem was that the position of the cursor was at the end of the resultset, so my attempts to iterate through it resulted in no action (and, consequentially, my UI not being populated).

    The solution that worked was to move the cursor position to back before first:

    // Move cursor before first so we can still iterate after config change
    cursor.moveToPosition(-1);
    

    I put that at the start of my onLoadFinished method.

提交回复
热议问题