viewpager savedinstancestate 中的片段始终为空

Nob*_*bbe 5 android android-fragments android-viewpager android-bundle

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

我有一个 viewpager,它填充了来自 FragmentStatePagerAdapter 的片段(android.support.v4.app.Fragment)。其中一些片段包含在方向更改时需要保留的逻辑,例如跟踪当前选择了哪个视图。

但是,尽管我将相关数据保存在 onSaveInstanceState 中,但savedInstanceState 始终为空。我可以通过将数据存储在一个静态变量中来解决这个问题(因为我每个片段只有一个实例对我有用)但我发现这是一个非常丑陋的解决方案,必须有一种正确的方法来做到这一点。

这是在旋转时不保留其状态的片段之一:

    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));
    }


}
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题而不必将我的状态保存在静态变量中?

编辑

我应该指出,所有片段都是以编程方式创建的,因此它们没有 id,我读到这可能是问题所在,但我也不知道如何解决。

此外,我的应用程序的结构如下:

  • 带有 NavigationDrawer 的 MainActivity
    • 片段1
      • 浏览器
        • 亚片段 1 - 亚片段 5
    • 片段2
    • 片段3

我遇到问题的状态的片段是子片段。

Dre*_*gen 4

Activity托管您的文件中Fragment,您需要存储对片段的引用Bundle

像这样的东西应该适合你

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);
}
Run Code Online (Sandbox Code Playgroud)

fragment1是您在问题中提到的需要重新创建的 Fragment1 实例。

我以前没有用过像你这样的结构,但这就是我开始的方式:

onSaveInstanceState您的 中Fragment1,我相信您需要对 中的每个片段执行相同的操作ViewPager。然后在onCreateViewFragment1从片段管理器获取片段并重新创建您的ViewPager.

我在这里找到了这个答案,它几乎相同,但有更多细节:/sf/answers/1199474251/