Fragment comments

来源:互联网 发布:linux java开发 编辑:程序博客网 时间:2024/05/17 04:22

1. Fragment

     实现一个UI, 可在activity之间共享

     实现一个逻辑

2. Fragment 在activity的控制下,没有activity不能活动

3. 引入Fragment要解决的问题

      Fragment和activity的通信

      Fragment之间的通信

     Fragment 和back stack

     Fragment和activty的action bar

4. Fragment的生命周期

The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.

每次fragment被切换出去又切换回来,会重新调用此函数。目的可能是为节省内存

  1. onActivityCreated(Bundle) tells the fragment that its activity has completed its ownActivity.onCreate().

每次fragment被切换出去又切换回来,不会调用此函数

  1. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.

每次fragment被切换出去又切换回来,不会调用此函数

  1. onStart() makes the fragment visible to the user (based on its containing activity being started).
  2. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
  2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment's state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

5. Fragment和Activity的关联

1。在Activity的layout文件中,fragment象一个view一样被使用,但是他不是view,每个Fragment应有不同的id

2。动态使用FragmentManager增加 ,这方法可加入一个无UI的Fragment

Fragment通过getActivity获得相关联的Activity


6。 FragmentManager,Fragment的管理者

Activity的Fragment通过FragmentManager管理。

查询和pop stack的管理

增加和删除,替换,fragment, fragment显示的控制

?attach/detach


7。 Fragment和activity的action bar

Fragment

setHasOptionsMenu()

onCreateOptionsMenu()

onOptionsItemSelected()

onCreateContextMenu()

onContextItemSelected()

菜单项加在最后

7。 Fragment的参数传递

setArguments(Bundle args)


 


0 0
原创粉丝点击