Fragment(小节)

Fragment

source page:http://blog.csdn.net/flowingflying/article/details/12505453

  • 为了pad引入在同一个平面上处理更多的内容
  • 子activity,提供ui与代码的重用
  • fragment stack back按钮回退

 

*xml 元素中不能添加子view或者是

lifeCycle

  • onInflate() 没有与activity关联,一般处理一些fragment的属性或是参数,实际编程使用少(savedInstanceState)
  • onAttach() 关联activity时调用
  • onCreate() fragment初始化,建议在此处可以建立后台线程
  • onCreatView() 创建fragment,并返回fragment的view层次结构,可以执行inflate findViewById等操作
  • onViewCreated Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, This gives subclasses a chance to initialize themselves.The fragment's view hierarchy is not however attached to its parent at this point.(对于在onCreatView()中生成的view 对象可以根据对应的bundle 信息初始化对应的状态)
    (以上均在Activity setContentView中进行,如果你在xml中添加了)
  • onActivityCreated() 在activity onCreate之后调用,fragment与activity相互处理(setListAdapter(new Adapter(getActivity)]))等。
  • onStart()
  • onResume()
  • onPause()
  • onStop() 相似于Activity
  • onDestoryView() view相关的资源释放时调用onDestroyView(),清空通过onCreateView()所返回的view
  • onDestroy() fragment状态清空 bundle
  • onDetach() 删除activity关联

Tips

    • setArguments() (需要在onAttach之前)
    • onCreatView() View v = inflater.inflate(R.layout.details, container,false);//注意:第3个参数为false,不要将view和viewGroup进行关联,系统会自动进行,否则会出现异常。
    • ft.addToBackStack(null);//加入 back stack
    • ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    • ft.commit()//任何对fragments的操作需要添加commit
posted @ 2013-11-21 10:23  Jsaint  阅读(225)  评论(0)    收藏  举报