Android and relive the life cycle Fragment

Android under review and Fragment life cycle, understanding the role of the life-cycle approach, when to call, what can do some operations.

1, Android life cycle

Figure 1.1 Lifecycle

image

1.2 Life Cycle Function Description

  • the onCreate : Activity is being created, the first life-cycle methodology, setContentView need to load layout. Here we do not recommend consuming operation, only layout and control is initialized. The method is passed a Bundle object can get a state of Activity.
  • onStart : Activity is starting, this time Activity already is visible, but not in the front display, and therefore can not interact with the user, where you can also initialize the control, but the official recommended in onCreate. If Activity to the foreground, then followed onResume, if Activity become hidden, then followed onStop.
  • onResume : Activity has been visible in the foreground, you can interact with the user, and Activity in the top of the stack, in this initialization can be some resources, followed by onPause method.
  • onPause : Called when a jump to another activity, this method can do some animation stops or data storage or recycling operations, but not too time-consuming, because android specified onPause has not been completed within 500ms, they would have forced shut Activity . If the Activity returns foreground, then followed onResume, if Activity invisible to the user into the state, followed by onStop.
  • onStop : Activity is about to stop or represent completely covered, this time Activity invisible, only run in the background, which is still in memory, has not been destroyed, what can be done non-time-consuming operation. If Activity resume interaction with the user, then followed onRestart, if Activity is destroyed, then followed onDestroy
  • onRestart : Activity visible at this time, when users press the Home key to switch to the desktop and then cut back or an Activity will trigger an Activity from this method before and after the switch back in, always followed by onStart.
  • onDestory : Activity is destroyed before the call, the end of the activity (call to finish), or not enough to be called when the destruction of activity of system memory, can distinguish between these two cases according to isFinishing () method. Do recovery work and the final release of resources.

    2, Fragment life cycle

    Figure 2.1 Lifecycle

    image

    2.2 Life Cycle Function Description

    fragment when they were created, experience include onAttach, onCreate, onCreateView, onActivityCreated method; fragment visible to the user when experience includes onStart, onResume method; fragment into the "background mode" when experience onPause, onStop method; fragment was destroyed (or hold its activity is destroyed), subjected to contain onPause, onStop, onDestroyView, onDestroy, onDetach method; and may be subject onCreate, onCreateView, onActivityCreated method Bundle object stored in a fragment.
  • onAttach : Called when Fragment occurrence associated with Activity
  • onCreate:创建Fragment时被回调,经历暂停或停止状态继而恢复后,想保留Fragment的基本组件,则在此进行初始化。
  • onCreateView:首次绘制页面时候调用,在此可以创建View,也可以返回null,这样不建议耗时操作。
  • onActivityCreated:Fragment绑定Activity,在onCreate方法已经执行完成并返回,在该方法内可以进行与Activity交互的UI操作,不能在此之前跟Activity进行交互。
  • onStart:启动 Fragment 时被回调,此时Fragment可见,只是还没有在前台显示,因此无法与用户进行交互
  • onResume:Fragment在前台可见,处于活动状态,用户可与之交互
  • onPause:Fragment处于暂停状态,但依然可见,用户不能与之交互
  • onStop:停止Fragment回调,Fragment完全不可见
  • onDestoryView:销毁与Fragment有关的视图,但未与Activity解除绑定
  • onDestory:销毁 Fragment 时被回调,通常按Back键退出或者Fragment被回收时调用此方法,此后接onDetach
  • onDetach:与onAttach相对应,当Fragment与Activity关联被取消时调用
  • setUserVisibleHint : Fragment ways to set call times visible or invisible. You can call getUserVisibleHint () Fragment obtained visible or not visible, if visible lazy loading operation is performed

Guess you like

Origin www.cnblogs.com/fomin/p/11443454.html