应用暂停一段时间后 FragmentActivity 丢失
我在使用我一直在开发的 Android 应用程序时遇到一些奇怪的行为,发生的情况如下:
- 我使用该应用程序一段时间,工作正常,没有问题
- 我通过单击 Android 主页按钮“暂停”应用程序
- 我使用其他一些手机上的应用程序几分钟后,
- 我返回到我的应用程序,它似乎处于一种非常奇怪的状态,FragmentActivity 丢失了,但最后显示的 Fragment 仍然显示。当我从 Fragment 调用“getActivity()”时,它返回 null,这会导致问题(因为我需要引用回 FragmentActivity。
任何想法为什么 FragmentActivity 在应用程序暂停一段时间后会丢失,我认为它的一些有点垃圾收集问题,但通常整个应用程序不会被破坏而不仅仅是 FragmentActivity 吗?
需要注意的是:
- 我正在使用兼容性库
- ,我正在使用视图页面指示器(但我不认为这是有什么可做的)与问题) - https://github.com/JakeWharton/Android-ViewPagerIndicator
注意:我当我从清单中的活动元素中删除 android:configChanges="orientation|keyboardHidden|screenSize"
并进行方向更改时,也可以轻松重现此问题。 所以我认为它在活动娱乐后不会保持视图的状态。 onSaveInstanceState
和 onRestoreInstanceState
调用正常,并且 setRetainInstance(true);
在片段上设置,所以我不确定为什么会发生这种情况。
I am experiencing some weird behavior with an android app I have been working on, here is what happens:
- I use the app for a while, works fine without problems
- I "pause" the app by clicking on the android home button
- I use some other apps on the phone for a few mins
- I go back to my app and it seems like its in a very weird state, the FragmentActivity is lost, but the Fragment that was being displayed last it still shown. When i do a call "getActivity()" from the Fragment it returns null which causes problems (because i need a reference back to the FragmentActivity.
Any idea why the FragmentActivity is lost after the app is paused after a while, i assume its some sort of garbage collection issue, but normally wouldn't the whole app be destroyed not just the FragmentActivity?
Some things to note:
- I am using the compatibility library
- I am using viewpage indicator (but i dont see this as being anything to do with the issue) - https://github.com/JakeWharton/Android-ViewPagerIndicator
Note: i can also reproduce this problem easily when i remove my android:configChanges="orientation|keyboardHidden|screenSize"
from the activity element in the manifest and do a orientation change.
So i assume its not maintaining state of the views after activity recreation.onSaveInstanceState
and onRestoreInstanceState
are called ok, and setRetainInstance(true);
is set on the fragment so im not sure why this is happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题似乎是 Maurycy 的问题 - 您所拥有的片段引用与 ViewPager 使用的片段引用不同,因为 Android 重新创建了一些片段并将您排除在循环之外。请查看我对他的问题的回答。
Your problem seems like that of Maurycy - the fragment references you have are not the same as the ones used by the ViewPager because Android has recreated some fragments and has left you out of the loop. Please have a look at my answer to his question.