FragmentStatePageAdapter不会删除子片段

时间:2015-06-25 15:33:25

标签: android android-fragments fragmentstatepageradapter fragmentmanager

问题FragmentStatePagerAdapter和屏幕更改方向(我猜是保存状态)。 我的应用程序的布局如下:

Activity -->
         Fragment A
         Fragment B (contains a FragmentStatePagerAdapter) wich shows 2 fragments 
                  --> 
                    Fragment B1 
                    Fragment B2

所有提到的片段都来自BaseFragment类,它没有做任何特殊的事情,但保留了一些" global"我需要的变种。 当Activity开始加载Fragment A时。从菜单中我将Fragment A替换为Fragment BFragment B来源如下:

public class Fragment B extends BaseFragment {

    public static String TAG = "b_fragment";
    private MyCustomPagerAdapter adapter;
    private ArrayList<String> tags = new ArrayList<>();
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new MyCustomPagerAdapter(getFragmentManager());
        //adapter = new MyCustomPagerAdapter(getChildFragmentManager());
        tags.add(FragmentB1.TAG);
        tags.add(FragmentB2.TAG);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(adapter);
        SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout)view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setViewPager(mViewPager);        
    }

    class MyCustomPagerAdapter extends FragmentStatePagerAdapter {
    //class MyCustomPagerAdapter extends FragmentPagerAdapter {

        FragmentManager mgr;
        public SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

        public PrepagatePagerAdapter(FragmentManager mgr) {
            super(mgr);
            this.mgr = mgr;
        }

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new FragmentB1();
                case 1:
                    return new FragmentB2();
            }
            return null;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)  {
            Fragment fragment = (Fragment)  super.instantiateItem(container, position);
            registeredFragments.put(position, fragment);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            registeredFragments.remove(position);
            super.destroyItem(container, position, object);
        }

        public Fragment getRegisteredFragment(int position) {
            return registeredFragments.get(position);
        }
    }
}

当我转到Fragment B然后再次返回Fragment A并更改方向时,会出现问题:我在onAttach()的{​​{1}}方法调试中看到Fragment A FragmentManager不再存在(替换为Fragment B),但Fragment AFragment B1在那里并被称为他们的B2方法,结果在布局的奇怪行为。不应该从onAttach()移除子片段吗?

目前我还不知道是否需要更多细节。如果需要,我可以进一步提供。

0 个答案:

没有答案