Android ListFragment 不会在 onSaveInstanceState() 中保存包/不会在 onActivityCreated() 中检索包

发布于 2024-11-26 15:09:26 字数 1322 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池木 2024-12-03 15:09:26

我遇到了同样的问题,将 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.

安静 2024-12-03 15:09:26

确保您没有在 Fragment 中调用 setRetainInstance(true)。经过一些实验后,我发现这是我的代码中的错误。必须这样做的缺点是必须手动捆绑所有实例数据。

删除方法调用并更新 onSaveInstanceState 以打包所有实例变量后,我现在可以恢复旋转时的列表位置。

Make sure you're not calling setRetainInstance(true) in the Fragment. 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.

微暖i 2024-12-03 15:09:26

我遇到了同样的问题,最终我发现它是在两种不同布局(纵向和横向)的片段元素上具有不同的 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文