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