如果AsyncTaskLoader运行过程中的方向发生了变化LoaderCallbacks.onLoadFinished不叫 [英] LoaderCallbacks.onLoadFinished not called if orientation change happens during AsyncTaskLoader run

查看:191
本文介绍了如果AsyncTaskLoader运行过程中的方向发生了变化LoaderCallbacks.onLoadFinished不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Android的支持,v4.jar和FragmentActivity(无片段在这一点)

Using android-support-v4.jar and FragmentActivity (no fragments at this point)

我有我开始加载,然后改变方向,而后台线程仍在运行的AsyncTaskLoader。在我的日志我看到的响应来通过背景的要求。的回答完全和我期待onLoadFinished()被调用,但它永远不会是。

I have an AsyncTaskLoader which I start loading and then change the orientation while the background thread is still running. In my logs I see the responses come through to the background requests. The responses complete and I expect onLoadFinished() to be called, but it never is.

由于故障排除的一种手段,在清单中,如果我设置的android:configChanges =方向onLoadFinished()被调用如预期

As a means of troubleshooting, in the Manifest, if I set android:configChanges="orientation" onLoadFinished() gets called as expected.

我的活动实现了装载机回调。在源LoaderManager.initLoader()我看到,如果加载程序已经存在,新的回调被设置用于LoaderInfo内部对象类,但我没有看到Loader.registerListener()再次调用。 registerListener似乎只在LoaderManagerImpl.createAndInstallLoader()被调用被调用。

My Activity implements the loader callbacks. In the source for LoaderManager.initLoader() I see that if the loader already exists, the new callback is set to the LoaderInfo inner object class but I don't see where Loader.registerListener() is called again. registerListener only seems to be called when LoaderManagerImpl.createAndInstallLoader() is called.

我怀疑,因为该活动被破坏并重新创建的方向变化,因为它是侦听器回调,新的活动未注册的通知。

I suspect that since the activity is destroyed and recreated on orientation change and since it is the listener for callbacks, the new activity is not registered to be notified.

任何人都可以证实我的理解,有什么办法解决,这样onLoadFinished方向更改后,叫什么名字?

Can anyone confirm my understanding and what the solution so that onLoadFinished is called after orientation change?

推荐答案

尼古拉确定的问题 - 谢谢

Nikolay identified the issue - Thank you.

我打电话initLoader弗朗onResume()。 Android的文件规定:

I was calling initLoader fron onResume(). The Android documentation states:

你通常初始化活动的onCreate内的装载机()   法,或在片段的onActivityCreated()方法。

"You typically initialize a Loader within the activity's onCreate() method, or within the fragment's onActivityCreated() method."

阅读典型作为多一点有力的比我当谈到与配置更改的生命周期。

Read "typically" as a bit more emphatic than I did when it comes to dealing with configuration change life cycle.

我把我的initLoader调用的onCreate(),并解决了问题。

I moved my initLoader call to onCreate() and that solved my problem.

我想原因是,在FragmentActivity.onCreate()LoaderManagers集合从LastNonConfigurationInstance和FragmentActivity.onStart拉()有一些启动有关装载机和LoaderManagers工作。事情已在处理的时候onResume()被调用。当装载需求实例化的第一次,调用initLoader来自外部的onCreate()仍然有效。

I think the reason is that in FragmentActivity.onCreate() a collection of LoaderManagers is pulled from LastNonConfigurationInstance and in FragmentActivity.onStart() there is some start up work regarding Loaders and LoaderManagers. Things are already in process by the time onResume() is called. When the Loader needs instantiated for the first time, calling initLoader from outside onCreate() still works.

这篇关于如果AsyncTaskLoader运行过程中的方向发生了变化LoaderCallbacks.onLoadFinished不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆