大牛们,请教个Fragment生命周期的问题,我在程序中调用detach(),看日志,AFragment执行了onDestroyView(),但之后Fragment切换时候,我再调用attach() 时候看AFragment没有调用onCreateView(),只调用了onActivityCreated(),不知道为什么,我没把Fragment addToBackStack,和这个有关系么Fragment生命周期

解决方案 »

  1.   

    没人知道吗? 
    fragment = fm.findFragmentById(R.id.enter_afragment);
    ft.attach( fragment );
    ft.detach(mCurrentFragment);
      

  2.   

    听说回帖可以得积分,fragment用过,但是调用了常用的2个方法,这个真心不懂!
      

  3.   

    刚看了一下文档,
    onCreateView()
    The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.这里面说到了一个the first time。第一次已经调用了这个方法,虽然你又调用了detach,他会调用
    onDestroyView()
        Called when the view hierarchy associated with the fragment is being removed.
    然而你再调用attach()的时候,系统的GC还没有去回收,又或者你某个地方的引用还在系统GC不会去回收这个view,所以你再调用attach的时候,他已经不是the first time了,就不会调用OnCreateView了。这只是我个人看了文档之后的见解,有可能有误,仅供参考