使用支持库v4 rev 20时,未调用子片段的onCreateView()

时间:2014-07-03 08:53:29

标签: android android-fragments android-support-library

我对手机的看法是这样的:

MainActivity包含PaymentsFragment,其中包含FragmentPagerAdapter,其中包含2 TransactionsListFragments

PaymentFragment示例代码:

  @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);


        mPaymentsPagerAdapter = new PaymentsPagerAdapter(getActivity(), getChildFragmentManager(), this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_payments, container, false);
        assert view != null;


        if (!isFirstRun)
            mPaymentsPagerAdapter.setOnTransactionClickListener(this);
        else
            isFirstRun = false;
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.fragment_payments_viewpager);
        viewPager.setAdapter(mPaymentsPagerAdapter);
        PagerSlidingTabStrip pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.fragment_payments_tabstrip);
        pagerSlidingTabStrip.setViewPager(viewPager);


        return view;
    }

PaymentsPagerAdapter示例代码:

  @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
                TransactionsListFragment recurringPaymentsListFragment = TransactionsListFragment.newRecurringPaymentsInstance();
                recurringPaymentsListFragment.setOnTransactionClickListener(mPaymentsFragment);
                return recurringPaymentsListFragment;

            case 1:
                TransactionsListFragment paymentsListFragment = TransactionsListFragment.newPaymentsInstance();
                paymentsListFragment.setOnTransactionClickListener(mPaymentsFragment);
                return paymentsListFragment;
        }
        return null;
    }

TransactionsListFragment内,没有什么事情发生。此外,它的onCreate()和onCreateView()都没有被调用,因此我认为PaymentsFragmentPaymentsPagerAdapter中存在错误。

问题是,在升级到Support v4 lib rev20之前,所有代码都运行良好。我降级到rev19,它又恢复了工作。那么,关于导致它在第20版中失败的原因的任何想法,还是一个错误?另外,如果有的话,解决方法是什么?

1 个答案:

答案 0 :(得分:0)

显然,从v20开始,支持库中的setRetainInstance()方法发生了一些变化,继续在Android 5.0上与v21一起。

一旦我从父片段中删除了setRetainInstance(true),就可以了。