setOffscreenPageLimit(int)在屏幕旋转时不起作用

时间:2013-09-17 15:56:26

标签: android android-viewpager

我试图保存viewpager中包含的表单数据。当我处于landscape模式时,它工作正常,但是当我旋转屏幕时,所有数据都是走了,田地再次空白。怎么处理?

我称之为viewpager的片段:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_main,
                container, false);
mViewPager = (ViewPager)rootView.findViewById(R.id.pager);
        mAdapter = new MyFragmentPagerAdapter1(getChildFragmentManager());
        mViewPager.setOffscreenPageLimit(6); 

        new setAdapterTask().execute();
        return rootView;
    }
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
          protected Void doInBackground(Void... params) {
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                       mViewPager.setAdapter(mAdapter);
            }
    }

adpater:

@Override
    public Fragment getItem(int arg0) {


        switch (arg0) {
        case 0:
            Part1 p1= new Part1();
            p1.setRetainInstance(true);
            return p1;
        case 1:
            return new part2();
        default:
            return null;
        }
    }

2 个答案:

答案 0 :(得分:0)

您是否尝试使用Fragment保留setRetainInstance(true)的实例?

答案 1 :(得分:0)

解决方案只是保存onSaveInstanceState(Bundle outState)中的数据并将其加载到OnViewCreated