Android ListFragment 不会在 onSaveInstanceState() 中保存包/不会在 onActivityCreated() 中检索包
我是 Android 新手,面临以下问题。我正在为 Android 2 和 Android 3 进行开发,这就是我使用片段的原因。但是,为了使应用程序在 Android 2 设备上运行,我导入了 android.support.v4.app.ListFragment。当屏幕方向改变时,我需要在 ListFragment 中保持选择。我正在重写 onSaveInstanceState() 方法并将 int 放入包中。当屏幕旋转时,调用此方法并将 int 添加到捆绑中。然而,当调用onActivityCreated()
时,它的bundle为null。我正在遵循Android网站上提供的示例: http://developer.android.com /reference/android/app/Fragment.html,但如上所述 - 调用onSaveInstanceState()
后,onActivityCreated()
中的bundle仍然为null 。
这是代码:
import android.support.v4.app.ListFragment;
public class VisitsHomeFragment extends ListFragment {
private int selectedPosition = -1;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("SELECTED_POSITION")) {
selectedPosition = savedInstanceState.getInt("SELECTED_POSITION");
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("SELECTED_POSITION", selectedPosition);
}
}
如果您能解决这个问题,我将不胜感激。
I'm new to android and I'm facing the following problem. I'm developing for both, Android 2 and 3, and this is why I use fragments. However to make the app working on Android 2 devices I import android.support.v4.app.ListFragment. I need to maintain selection within my ListFragment when orientation of the screen changes. I'm overriding onSaveInstanceState()
method and put an int into the bundle. When the screen is rotated, this method is called and the int is added to the bundle. However when onActivityCreated()
is called, its bundle is null. I am following the example provided on Android website: http://developer.android.com/reference/android/app/Fragment.html, but as mentioned above - after onSaveInstanceState()
is called, the bundle in onActivityCreated()
is still null.
Here's the code:
import android.support.v4.app.ListFragment;
public class VisitsHomeFragment extends ListFragment {
private int selectedPosition = -1;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("SELECTED_POSITION")) {
selectedPosition = savedInstanceState.getInt("SELECTED_POSITION");
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("SELECTED_POSITION", selectedPosition);
}
}
I would appreciate any help with this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,将
android:id
添加到布局文件中的片段元素修复了此问题。看来
FragmentManager
在重新创建片段时使用 id 发送适当的包。I had the same problem, adding
android:id
to the fragment element in the layout file fixed this issue.It seems
FragmentManager
uses the id to send the appropriate bundle when recreating a fragment.确保您没有在
Fragment
中调用setRetainInstance(true)
。经过一些实验后,我发现这是我的代码中的错误。必须这样做的缺点是必须手动捆绑所有实例数据。删除方法调用并更新
onSaveInstanceState
以打包所有实例变量后,我现在可以恢复旋转时的列表位置。Make sure you're not calling
setRetainInstance(true)
in theFragment
. After a little experimentation I pinpointed this as being the error in my code. The downside of having to do it this way is one has to manually bundle all instance data.After removing the method call and updating my
onSaveInstanceState
to parcel all of my instance variables, I can now restore list position on rotation.我遇到了同样的问题,最终我发现它是在两种不同布局(纵向和横向)的片段元素上具有不同的 android:id 属性值。
I had the same problem, and I eventually tracked it down to having different android:id attribute values on the fragment elements in the two different layouts (portrait and landscape).