2014-07-03 58 views
0

時候不要叫我有一個是這樣的手機上一個觀點:兒童片段的onCreateView()使用支持LIB V4 rev20

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版中失敗了,還是它是一個錯誤?此外,如果有的話,解決方法是什麼?

回答

0

顯然,支持庫中的setRetainInstance()方法隨着v20開始發生了一些變化,與Android 5.0一起繼續v21。

一旦我從父代片段中刪除setRetainInstance(true),它就起作用了。