有时调用initLoader后没有得到onCreateLoader回调
我有一个活动在创建时调用 initLoader 以便加载该活动将显示的数据。通常情况下,这工作得很好。我设置了断点来查看一切发生的顺序。首先我调用 initLoader,然后得到 OnCreateLoader 回调,然后是 OnLoadFinished 回调,然后一切都很好。
但是,我注意到,如果我更改 Android 上的方向,我不会收到 OnCreateLoader 回调或 OnLoadFinished 回调,从而导致页面上没有显示任何数据。我仍然在 initLoader 调用上得到断点,所以我知道这种情况正在发生,但我没有收到错误,也找不到任何文档解释为什么在调用 initLoader 后我不会得到回调。
有趣的是,如果我返回到原始方向,我确实会收到回调。
所以,总结一下:
我以纵向启动活动
- 我调用 initLoader
- 我得到 onCreateLoader 和 onLoadFinished 回调
我然后更改为横向
- 我调用 initLoader
- 没有 onCreateLoader 或 onLoadFinished 回调
我改回纵向
- 我调用 initLoader
- 我得到 onCreateLoader 和 onLoadFinished 回调
类似地,如果我以横向模式启动活动,我会收到回调。然后,如果我切换到肖像,我就不会。然后,如果我切换回横向,我会再次收到回调。
有人知道发生了什么事吗?
I have an activity that calls initLoader when it is created in order to load the data that the activity will display. Normally, this works fine. I have breakpoints set to see the order that everything happens. First I call initLoader, then I get the OnCreateLoader callback, then the OnLoadFinished callback, then everything is great.
However, I noticed that if I changed the orientation on my Android that I don't get the OnCreateLoader callback or the OnLoadFinished callback, resulting in no data displayed on the page. I still get my breakpoint on the initLoader call so I know that is happening, but I don't get an error and I can't find any documentation explaining why I wouldn't get a callback after calling initLoader.
Interestingly, I do get the callbacks if I return to the original orientation.
So, to summarize:
I start the activity in portrait orientation
- I call initLoader
- I get onCreateLoader and onLoadFinished callbacks
I then change to landscape
- I call initLoader
- no onCreateLoader or onLoadFinished callbacks
I change back to portrait
- I call initLoader
- I get onCreateLoader and onLoadFinished callbacks
Similarly, if I start the activity in landscape mode I get the callbacks. Then, if I switch to portrait, I don't. Then, if I switch back to landscape I get the callbacks again.
Does anyone have any idea what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我们在 ViewPager 中的 Fragment 中也看到过同样的问题。当片段恢复时,加载程序似乎没有再次正确激活。事实上,当片段暂停时,我们的 CursorLoader 不再像我预期的那样获得 ContentObserver 回调。
我们的解决方法是调用 restartLoader() 来转储以前的内容并重新加载数据。这与 destroyLoader() 具有相同的净影响; initLoader(),但是效率会更高。
We've seen this same problem with Fragments in a ViewPager. It appears that the loaders are not properly activated again when a fragment is resumed. In fact, when the fragment is paused, our CursorLoader no longer gets ContentObserver callbacks as I would expect.
Our workaround has been to call restartLoader() which dumps the previous contents and reloads the data. This would have the same net affect as destroyLoader(); initLoader(), but it will be more efficient.
您需要在 onCreate 中调用 initLoader
对于类似的问题和更多信息,请查看 如果在 AsyncTaskLoader 运行期间发生方向变化,则不会调用 LoaderCallbacks.onLoadFinished
You'll need to call initLoader in your onCreate
For a similar issue and more info have a look at LoaderCallbacks.onLoadFinished not called if orientation change happens during AsyncTaskLoader run
我一直在追踪这个问题
初始创建(OnActivityCreated)
initLoader -> OnCreateLoader-> OnOnStartLoading-> OnLoadFinished
方向更改 - OnActivityCreated 再次调用
initLoader - OnLoadFinished(不加载任何数据!) - bug。
我发现的唯一解决方法是在 init 之前调用 destroyLoader() 。这会强制发生正确的创建/开始/完成序列。
似乎正在发生的事情是 initLoader 代码被编写为假设片段在方向改变期间没有被破坏,这当然不是 android 的其余部分的工作方式。
I've been tracing this problem
Initial create (OnActivityCreated)
initLoader -> OnCreateLoader -> OnOnStartLoading -> OnLoadFinished
Orientation change - OnActivityCreated called again
initLoader - OnLoadFinished (Without loading any data!) - bug.
The only workaround I've found is to call destroyLoader() just before the init. This forces the proper Create/Start/Finished sequence to happen.
What appears to be happening is the initLoader code is written to assume the fragment isn't destroyed during an orientation change, which is of course not how the rest of android works..
我在方向改变的片段中的光标适配器上发生了非常类似的事情。 initloader 在 onCreateView 中被调用(也尝试了 onStart 和 onResume),但 onCreatLoader 以及其他加载器回调方法从未被调用。另外值得注意的是,在我的例子中,该片段包含在视图寻呼机中。
因此,最初创建了活动、视图寻呼机和片段,一切正常。在方向改变时,虽然我的片段中的列表不会获取数据。再次改变方向,数据就回来了。
就我而言,修复方法是更改此内容:
我将其更改为此
I had something very similar thing happening with cursor adapters in fragments on orientation change. initloader was called in onCreateView (also tried onStart and onResume) but onCreatLoader as well as the other loader callback methods never were called. Also of note in my case the fragment was contained in a view pager.
So initially the activity and view pager and fragments were created and everything worked. On orientation change though the lists in my fragments would not get data. Changing orientation again and the data came back.
In my case the fix was changing this:
I changed it to this
我花了几周的时间来跟踪这个问题。把以前的片段内容放到我的新片段中很累。
终于修好了。
在我的 onCreate() 方法(其中片段实现了 LoaderManager)中,我执行了此
操作,当新加载器未正确激活时它会起作用。
I spent weeks on tracking this issue. It tired to put the previous fragment content to my new fragment.
Finally fixed.
In my onCreate() method of which fragment implemented LoaderManager, I did this
It works when new loader was not actived properly.
我最近遇到了这个问题,想提交我的解决方案。
我发现只需执行以下操作就足够了:
I had this problem recently, and would like to submit my solution.
I found it was simply enough to just do:
这是一个老问题,但也许有人仍然关心......
我有同样的问题并且能够解决它。这是链接:方向更改时不调用 onCreateLoader
This is an old question, but maybe someone might still cares...
I had the same problem and was able to fix it. Here's the link: onCreateLoader not called when orientation changes