0

我正在开发一个由视图寻呼机组成的应用程序,而我正在刷视图寻呼机,所有片段都正确加载,但这是我的问题。当我再次使用相同的片段滑动查看寻呼机时,它会再次加载,但根据我的要求,之前加载的片段应该得到恢复。我尝试了以下两种方式的代码。

A) 这是我的视图寻呼机

mViewPager = (ViewPager)mMyPageFragment.findViewById(R.id.Viewpager);

        myAdapter = new MyAdapter(getFragmentManager());
        mViewPager.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();

在适配器中,

public class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        System.out.println("MyAdapter="+position);
        switch (position) {
        case 0:
            return new MyPageFirstFragment1();
        case 1:
            return new MyPageFirstFragment2();
        case 2:
            return new MyPageFirstFragment3();

        default:
            return null;
        }
    }
}

第二种方法

private static Hashtable<Integer, Fragment> fragmentItems = new Hashtable<Integer, Fragment>();

    @Override
    public Fragment getItem(int position) {
        System.out.println("MyAdapter=" + position);
        switch (position) {
        case 0: {
            if (!fragmentItems.containsKey(position)) {
                fragmentItems.put(position, new MyPageFirstFragment1());
            }
            return fragmentItems.get(position);
        }
        case 1: {
            if (!fragmentItems.containsKey(position)) {
                fragmentItems.put(position, new MyPageFirstFragment2());
            }
            return fragmentItems.get(position);
        }
        case 2: {
            if (!fragmentItems.containsKey(position)) {
                fragmentItems.put(position, new MyPageFirstFragment3());
            }
            return fragmentItems.get(position);
        }
        case 3: {
            if (!fragmentItems.containsKey(position)) {
                fragmentItems.put(position, new MyPageFirstFragment4());
            }
            return fragmentItems.get(position);
        }

        default:
            return null;
        }
    }

这是我使用的代码,但我无法保留片段。

4

1 回答 1

1

you should not save your fragments into static object.. because fragment manager saves info about your fragments during device configuration changes and restores your fragments after that.. but into new Fragment objects. And your "old" static objects are becoming invalid. I prefer check in fragment activity onCreate() method if savedInstanceState != null and if it is I restore view pager fragments by there tags (findFragmentByTag) and create new adapter from restored fragments.. for Example `

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i( LOG_TAG, "onCreate()" );
        //setting view pager
        mViewPager = (ViewPager) findViewById( R.id.mainActivity_viewPager );


        //initializing activity
        if( savedInstanceState == null ) {
            Log.i( LOG_TAG, "onCreate() - savedInstanceState == null" );
            //activity first creation
        } else {
            Log.i( LOG_TAG, "onCreate(), savedInstanceState != null, it is activity start after device reconfiguration.. " );


            //recreating viewpager adapter
            mViewPager.setAdapter( recreateParentFragmentListAdapter() );

        }

and
`

private ParentFragmentListAdapter recreateParentFragmentListAdapter() {
        List<BaseParentFragment> fragments = new ArrayList<BaseParentFragment>(3);
        for( int id = 0; id < 3; id++ ) {
            String fragmentTag = getFragmentTag( R.id.mainActivity_viewPager, id );//int -> long converting
            BaseParentFragment fragment = (BaseParentFragment) (getSupportFragmentManager().findFragmentByTag( fragmentTag ));

            Log.i( LOG_TAG, "onCreate() - finding fragment with tag {" + fragmentTag + "}, found fragment = " + (fragment == null ? "null" : fragment.toString() ) );

            if( fragment == null ){//creating it
                switch( id ) {
                case 0:
                    fragment = new Tab1Fragment();
                case 1:
                    fragment = new Tab2Fragment();
                case 2:
                    fragment = new Tab3Fragment();
                }
            } 
            fragments.add( fragment );
        }

        ParentFragmentListAdapter adapter = new ParentFragmentListAdapter( getSupportFragmentManager(), fragments );
        return adapter;
    }

` and

private String getFragmentTag( int viewId, long id ) {
        return "android:switcher:" + viewId + ":" + id;
    }

from sdk

于 2013-02-18T14:00:47.220 回答