<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text = "@string/home"/> <fragment android:id = "@+id/fragment_space" android:name = "com.example.sxq123.iljmall.FragmentCatagorySpace" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </ScrollView> </LinearLayout>
public class FragmentHome extends Fragment { private View view; FragmentCatagorySpace fragmentCatagorySpace ; @Override public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInsataceState){ view = inflater.inflate(R.layout.fragment_home, null); Log.d(this.getClass().getName()," onCreateView() Begin"); initChildFragment(); return view; } private void initChildFragment(){ Log.d("-------------------","init space "); fragmentCatagorySpace = (FragmentCatagorySpace)getChildFragmentManager().findFragmentById(R.id.fragment_space); if(fragmentCatagorySpace != null){ Log.d("----------------","init space success and no null"); } } }
public class FragmentHome extends Fragment { private View view; FragmentCatagorySpace fragmentCatagorySpace ; @Override public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInsataceState){ view = inflater.inflate(R.layout.fragment_home, null); Log.d(this.getClass().getName()," onCreateView() Begin"); initChildFragment(); return view; } @Override public void onDestroyView(){ super.onDestroyView(); if(fragmentCatagorySpace != null){ Log.d("-------------------", "space no null"); FragmentManager fragmentManager = getFragmentManager(); if(fragmentManager != null && !fragmentManager.isDestroyed()){ final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction (); if(fragmentTransaction != null){ fragmentTransaction.remove(fragmentCatagorySpace).commit(); //commit()和commitAllowingStateLoss()都是先发送到主线程任务队列中, 当主线程准备好了再执行,异步。 // //若是没有在inflate以前执行remove child fragmetn 的transaction,将会发生duplicate id 的exception !!! // fragmentTransaction.commit();//能够放在onCreateView中执行commit(),但偶尔会发生onSaveInstanceState()以后不能执行commit()的冲突 fragmentTransaction.commitAllowingStateLoss(); //当即执行任务队列中的transaction, 不异步 !!!!!!!!!!!!!!!重点!!!!!!!!!!!!!!!!!!!! //防止remove事务被加到主线程任务队列中,那这样异步的话可能这些事物直到父Fragment从新执行onCreateView() //以前都没有执行完,一样会发生duplicate id 的异常 //若是这些remove 的食物放在父Fragment的onSaveInstanceState()中执行, 因为onSaveInstanceState()调用并不 //是每个Fragment生命周期都会被调用(????),因此偶尔也会发生duplicate id 的异常 fragmentManager.executePendingTransactions(); Log.d("------------------"," space destroy"); } } } } private void initChildFragment(){ Log.d("-------------------","init space "); fragmentCatagorySpace = (FragmentCatagorySpace)getChildFragmentManager().findFragmentById(R.id.fragment_space); if(fragmentCatagorySpace != null){ Log.d("----------------","init space success and no null"); } } }