onCreateLoader not called when orientation changes

后端 未结 4 1543
余生分开走
余生分开走 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:48

    It appears that this problem occurs when you combine some of activities from support libraries with android.app.LoaderManager.

    I had exactly the same issue with android.support.v7.app.ActionBarActivity and android.app.LoaderManager:

    • initial loading works
    • then when orientation changes loader newer finishes
    • and then when getting back to original orientation it works again.

    By using android.support.v4.app.LoaderManager instead, everything started working perfectly again!

    0 讨论(0)
  • 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<Cursor> 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.

    0 讨论(0)
  • 2021-01-18 03:58

    There was a work around documented on the question you referenced. Call restartLoader in onResume rather than create loader elsewhere. restartLoader will create if necessary.

    0 讨论(0)
  • 2021-01-18 04:04

    You can use LoaderManager.enableDebugLogging(true) to debug your loader behavior or you can try to use getSupportLoadermanager() instead of getLoaderManager()

    0 讨论(0)
提交回复
热议问题