滑动ViewPager时Fragment不调用onSaveInstanceState?

发布于 2024-12-20 00:12:32 字数 979 浏览 6 评论 0原文

我的 ViewPager 遇到问题,我的 ListView 失去了滚动位置。

ListView 的状态可以使用以下方法轻松存储和恢复:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.frag_cool_things, container, false);

    AdvListView listView = (AdvListView) v.findViewById(R.id.lv0);
    listView.setOnItemClickListener( mOnListItemClicked );

    if (null != savedInstanceState)
    {
        listView.onRestoreListViewInstanceState(savedInstanceState.getParcelable("list_state"));
    }

    mListView = listView;

    return v;
}

@Override
public void onSaveInstanceState (Bundle outState) 
{
    super.onSaveInstanceState(outState);

    outState.putParcelable("list_state", mListView.onSaveListViewInstanceState());
}

但是问题是,当片段被滑动时,onDestroyView() 被调用,但从未调用 onSaveInstanceState (Bundle outState)

旋转屏幕等可以很好地恢复 ListView 状态,但滑动我无法弄清楚如何正确恢复列表。

I'm having an issue with the ViewPager where my ListView is loosing it's scroll position.

The state of the ListView can easily be stored and restored using:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.frag_cool_things, container, false);

    AdvListView listView = (AdvListView) v.findViewById(R.id.lv0);
    listView.setOnItemClickListener( mOnListItemClicked );

    if (null != savedInstanceState)
    {
        listView.onRestoreListViewInstanceState(savedInstanceState.getParcelable("list_state"));
    }

    mListView = listView;

    return v;
}

@Override
public void onSaveInstanceState (Bundle outState) 
{
    super.onSaveInstanceState(outState);

    outState.putParcelable("list_state", mListView.onSaveListViewInstanceState());
}

However the problem is that when fragments are being swiped onDestroyView() gets called but never calls onSaveInstanceState (Bundle outState).

Rotating the screen and such restores the ListView state just fine but swiping I can't figure out how to restore the list properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

呆萌少年 2024-12-27 00:12:32

2011 年 12 月 17 日更新:
我实际上找到了保存片段内容的正确方法。您必须使用FragmentStatePagerAdapter。该适配器正确保存了片段的状态! :)

旧:
好吧,我找到了一种方法来做到这一点。如果您认为这是一个巨大的禁忌,请分享您的意见! :P

这是我的 FragmentBase 类,它解决了这个问题:

public abstract class FragmentBase extends Fragment
{
    private boolean mInstanceAlreadySaved;
    private Bundle mSavedOutState;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        if (null == savedInstanceState && null != mSavedOutState) {
            savedInstanceState = mSavedOutState;
        }

        mInstanceAlreadySaved = false;
        return onCreateViewSafe(inflater, container, savedInstanceState);
    }

    @Override
    public void onSaveInstanceState (Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        mInstanceAlreadySaved = true;
    }

    @Override
    public void onStop() 
    {
        if (!mInstanceAlreadySaved)
        {
            mSavedOutState = new Bundle();
            onSaveInstanceState( mSavedOutState );
        }

        super.onStop();
    }

    public abstract View onCreateViewSafe (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
}

Update 12/17/11:
I actually found the correct way to save the content of the Fragments. You must use FragmentStatePagerAdapter. This adapter properly saves the state of the fragment! :)

OLD:
Well I found a way to do this.. Please share your input if you believe this is a huge no no! :P

Here is my FragmentBase class that fixed this issue:

public abstract class FragmentBase extends Fragment
{
    private boolean mInstanceAlreadySaved;
    private Bundle mSavedOutState;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        if (null == savedInstanceState && null != mSavedOutState) {
            savedInstanceState = mSavedOutState;
        }

        mInstanceAlreadySaved = false;
        return onCreateViewSafe(inflater, container, savedInstanceState);
    }

    @Override
    public void onSaveInstanceState (Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        mInstanceAlreadySaved = true;
    }

    @Override
    public void onStop() 
    {
        if (!mInstanceAlreadySaved)
        {
            mSavedOutState = new Bundle();
            onSaveInstanceState( mSavedOutState );
        }

        super.onStop();
    }

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