标签:save 解决问题 activity str get linear ret actions 调用
<?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"); } } }
ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
标签:save 解决问题 activity str get linear ret actions 调用
原文地址:http://www.cnblogs.com/s-xq/p/6159600.html