片段viewpager savedinstancestate总是空 [英] Fragment in viewpager savedinstancestate is always null

查看:236
本文介绍了片段viewpager savedinstancestate总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前有人问,但没有到目前为止给出的答案是任何帮助我。

I know this question has been asked before but none of the answers given so far is of any help to me.

我有填充了从FragmentStatePagerAdapter片段(android.support.v4.app.Fragment)一viewpager。一些这些片段的包含需要时是当前选择的方向的变化,如跟踪哪些视图的要保留的逻辑。

I have a viewpager which is populated with fragments (android.support.v4.app.Fragment) from a FragmentStatePagerAdapter . Some of these fragments contain logic that needs to be retained when the orientation changes, such as keeping track of which view is currently selected.

不过,虽然我保存在问题的onSaveInstanceState的数据savedInstanceState总是空。我可以在一个静态变量存储数据(因为我只有每个片段的一个实例将工作对我来说),但我发现这是一个非常丑陋的解决方案,必须有这样做的正确的方法解决这个问题。

However, although I save the data in question in onSaveInstanceState the savedInstanceState is always null. I can solve this by storing the data in a static variable (which since I only have one instance of each fragment would work for me) but i found this to be a quite ugly solution and there has to be a proper way of doing this.

这是不保留它是在旋转状态的片段中的一个:

This is one of the fragments that doesn't retain it's state on rotation:

    public class PriceSelectFragment extends Fragment {

    private TableRow mSelected;
    private int mSelectedPos = 0;

    // newInstance constructor for creating fragment with arguments
    public static PriceSelectFragment newInstance() {
        PriceSelectFragment fragmentFirst = new PriceSelectFragment();
        return fragmentFirst;
    }

    public PriceSelectFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_price_select, container, false);
        TableLayout mTable = (TableLayout)view.findViewById(R.id.price_table);

        List<PriceGroup> mPriceGroups = ((MainActivity) getActivity()).getPriceGroups();

        int i = 0;
        for (final PriceGroup group : mPriceGroups) {
            //Create row from layout and access child TextViews
            TableRow r = (TableRow)inflater.inflate( R.layout.price_group, mTable, false);
            TextView size = (TextView)r.getChildAt(0);
            TextView dimension = (TextView)r.getChildAt(1);
            TextView weight = (TextView)r.getChildAt(2);
            TextView price = (TextView)r.getChildAt(3);

            //Populate row with PriceGroup Data
            size.setText(group.sizeIndicator);
            dimension.setText(String.format("%2.0fx%2.0fx%2.0f", group.length, group.width, group.height));
            weight.setText(Float.toString(group.weight));
            price.setText(Integer.toString(group.price));

            //Alternate background color every other row
            if (i % 2 == 0) {
                r.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_1));
            }
            else {
                r.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_2));
            }
            mTable.addView(r); // Add to table

            r.setTag(i);
            r.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectRow((TableRow) v);
                }
            });
            i++;
        }

        mSelected = (TableRow)view.findViewWithTag(mSelectedPos);
        selectRow(mSelected);

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("selected", mSelectedPos);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null) {
            mSelectedPos = savedInstanceState.getInt("selected");
        }
    }

    private void selectRow(TableRow row) {
        if ((int) mSelected.getTag() % 2 == 0) {
            mSelected.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_1));
        }
        else {
            mSelected.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_2));
        }
        mSelected = row;
        mSelectedPos = (int) mSelected.getTag();
        mSelected.setBackgroundColor(getResources().getColor(R.color.light_blue));
    }


}

我要如何解决这个问题,而不必救我的状态在静态变量?

How do I solve this without having to save my states in static variables?

我要指出的是,所有的碎片都编程创建,因此他们没有一个id,我读到这可能是问题,但我不知道如何解决,无论是。

I should point out that all of the fragments are programatically created and as such they do not have an id and I read that that might be the problem but I don't know how to solve that either.

此外我的应用程序的结构是这样的:

Also my application is structured like this:


  • MainActivity与NavigationDrawer

    • 片段1

      • ViewPager

        • subfragment1 - subfragment5

        片段我有麻烦是亚片段。其状态

        The fragments whose states I'm having trouble with are the subfragments.

        推荐答案

        在你的活动这是托管片段你需要一个refernce存储在片段中的捆绑

        In your Activity which is hosting your Fragment you need to store a refernce to the fragment in the Bundle.

        这样的事情应该为你工作。

        Something like this should work for you

        public void onCreate(Bundle savedInstanceState) {
        
            if (savedInstanceState != null) {
                //Restore your fragment instance
                fragment1 = getSupportFragmentManager().getFragment(
                            savedInstanceState, "fragment");
            }
        }
        
        
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
        
            getSupportFragmentManager().putFragment(outState, "fragment", fragment1);
        }
        

        片段1 是你在你的问题中提到的片段1需要得到重新的实例。

        fragment1 is the instance of the Fragment1 that you mentioned in your question that needs to get recreated.

        我还没有与以前像你这样的结构做到了这一点,但是这是我将开始:

        I haven't done this with a structure like yours before but this is how I would start:

        的onSaveInstanceState 片段1 我相信你需要做同样的,在每个片段你的 ViewPager 。然后在 onCreateView 片段1 从片段经理拿到的碎片并重新创建你的 ViewPager

        In the onSaveInstanceState in your Fragment1 I believe you would need to do the same with each of the fragments in your ViewPager. Then in the onCreateView on your Fragment1 get the fragments from the fragment manager and recreate your ViewPager.

        我已经在这里找到这个答案是pretty大致相同,但有一个更详细一点:<一href=\"http://stackoverflow.com/a/17135346/1417483\">http://stackoverflow.com/a/17135346/1417483

        I have found this answer here which is pretty much the same but has a little more detail: http://stackoverflow.com/a/17135346/1417483

        这篇关于片段viewpager savedinstancestate总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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