Fragment嵌套Fragment時遇到的那些坑


由於項目要改成MVP模式,自然會用到了Fragment,有時候可能會需要一個Fragment里面嵌套多個Fragment,並且add完成后需要立即刷新子Fragment的View,那么這個時候就會拋出異常,先看一段代碼:

MainActivity.java

  @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); //省略邏輯代碼。。。 mMessageFragment = MessageFragment.newInstance(); getSupportFragmentManager() .beginTransaction().add(R.id.layout_activity_main_fragment_contianer, mMessageFragment) .commitAllowingStateLoss(); }

MessageFragment.java

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_message, null); ButterKnife.bind(this, rootView); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initFragment(); updateFragment(); } private void updateFragment(){ for(int i=0;i<mMessageFragments.length();i++){ mMessageFragments[i].update(); } } private void initFragment() { replyFragment = ConversationListFragment.newInstance(false); replyFragment); privateFragment = ConversationListFragment.newInstance(true); privateFragment); mMessageFragments = new Fragment[]{ replyFragment, privateFragment}; getChildFragmentManager() .beginTransaction() .add(R.id.fragment_container, replyFragment) .add(R.id.fragment_container, privateFragment) .hide(privateFragment) .show(replyFragment) .commitAllowingStateLoss(); }

ConversationListFragment.java

 public static ConversationListFragment newInstance(boolean isprivate) { ConversationListFragment fragment = new ConversationListFragment(); Bundle args = new Bundle(); args.putBoolean("isprivate", isprivate); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mIsPrivate = getArguments().getBoolean("isprivate"); } } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_message_conversation_list, null); ButterKnife.bind(this, view); return view; } public void update(){ //當代碼運行到這里的時候就會發生錯誤 //比如:mIsPrivated的值是默認值,並沒有獲取到外部傳過來的值,如果邏輯代碼中需要用到View,則還會拋出空指針異常! if(mIsPrivated){ //省略邏輯。。。 }else{ //省略邏輯。。。 } }

分析原因

 相信只要仔細看過上面的代碼的都應該明白錯誤的原因了,主要原因就是對Fragment生命周期了解的不夠透徹,因為在父Fragment的onActivityCreated方法中雖然實例化了子Fragment但是子Fragment的生命周期方法並沒有被調用,所以這個時候子Fragment的View還沒有被創建,如果直接在父Fragment的onActivityCreated方法中調用子Fragment更新View的方法就會拋出異常!

解決方案

 既然知道了是因為子Fragment生命周期方法未執行引起的,那么就應該把更新子Fragmet的調用時機放到父Fragment的onActivCreated方法之外,可以放到父Fragment的onStart方法中即可!

總結

 其實主要是對Fragment生命周期方法不熟造成的,再次回憶總結一下Fragment生命周期方法:


1354170699_6619.png

ActivityFragment對比圖


1354170682_3824.png

生命周期分析:

  1. ragment被創建的時候
    • onAttach()
    • onCreate()
    • onCreateView()
    • onActivityCreated()
      2.fragment對用戶可見的時候
    • onStart()
    • onResume()
    • fragment進入“后台模式”的時候
    • onPause()
    • onStop()
    • fragment被銷毀的時候
    • onPause()
    • onStop()
    • onDestroyView()
    • onDestroy()
    • onDetach()
    • 就像activitie一樣,在以下的狀態中,可以使用Bundle對象保存一個fragment的對象。
    • onCreate()
    • onCreateView()
    • onActivityCreated()
    • fragments的大部分狀態都和activitie很相似,但fragment有一些新的狀態。
    • onAttached() —— 當fragment被加入到activity時調用(在這個方法中可以獲得所在的activity)。
    • onCreateView() —— 當activity要得到fragment的layout時,調用此方法,fragment在其中創建自己的layout(界面)。
    • onActivityCreated() —— 當activity的onCreated()方法返回后調用此方法
    • onDestroyView() —— 當fragment中的視圖被移除的時候,調用這個方法。
    • onDetach() —— 當fragment和activity分離的時候,調用這個方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM