In the previous Article blog post fragment made a brief introduction, now to explore the life cycle of fragment.
First, several states of fragment:
Similar to activity, fragment also has several states:
· Active Status: The current fragment is located in the foreground, visible, to get focus.
· Paused state: The other activity is in the foreground, and the fragment is visible and not focusable.
· Stop state: Not visible, loses focus.
· Destroy state: The fragment is completely deleted or ended.
Second, the fragment callback method:
In the life cycle of fragment, there is a way to get callbacks:
· Onattach (): Callback when fragment is added to activity, only once.
· OnCreate (Bundle savedstatus): Called when creating fragment, once.
· Oncreateview (): Callback Every time the view component of the fragment is created and drawn, fragment will display the view component returned by the method.
· Onactivitycreated (): Callback after the activity on which fragment is started is completed.
· OnStart (): Callback Every time the fragment is started or resumed.
· Onresume (): Callback when fragment is started or resumed, and this method is called back after the OnStart () callback.
· OnPause (): callback when paused.
· OnStop (): Callback when fragment is stopped.
· Ondestroyview (): callback When destroying the Fragment view component.
· OnDestroy (): Called when destroying fragment, once.
· Ondetach (): Fragment is removed from activity, returned to the method when replaced, and callback after OnDestroy ().
Depending on the timing of the callback, we can selectively override the specified method as needed. In order to achieve the relevant requirements.
Three, fragment life cycle instances
The following code is an example of the fragment life cycle, overriding the above 11 life cycle methods:
1@SuppressLint ("Newapi")2 Public classMyfragmentextendsFragment {3 Button Bnok;4 TextView tvShow;5 Private FinalString TAG = "Myfragment";6 7 Publicmyfragment () {8System.out.println (TAG + ": myfragment ()");9 }Ten One @Override A Public voidOnattach (activity activity) { - Super. Onattach (activity); -System.out.println (TAG + ": Onattach ()"); the } - - @Override - Public voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); -System.out.println (TAG + ": onCreate ()"); + A } at //override change method, return value view will be the component displayed as fragment - @Override - PublicView Oncreateview (layoutinflater inflater, ViewGroup container, - Bundle savedinstancestate) { -System.out.println (TAG + ": Oncreateview ()"); - //loading fragment_main.xml layout files inView Rootview = inflater.inflate (R.layout.fragment_main, container,false); - returnRootview; to } + - @Override the Public voidonactivitycreated (Bundle savedinstancestate) { * Super. onactivitycreated (savedinstancestate); $System.out.println (TAG + ": onactivitycreated ()");Panax Notoginseng } - the @Override + Public voidOnStart () { A Super. OnStart (); theSystem.out.println (TAG + ": OnStart ()"); + } - $ @Override $ Public voidOnresume () { - Super. Onresume (); -System.out.println (TAG + ": Onresume ()"); the } - Wuyi @Override the Public voidOnPause () { - Super. OnPause (); WuSystem.out.println (TAG + ": OnPause ()"); - } About $ @Override - Public voidOnStop () { - Super. OnStop (); -System.out.println (TAG + ": OnStop ()"); A } + the @Override - Public voidOndestroyview () { $ Super. Ondestroyview (); theSystem.out.println (TAG + ": Ondestroyview ()"); the } the the @Override - Public voidOnDestroy () { in Super. OnDestroy (); theSystem.out.println (TAG + ": OnDestroy ()"); the } About the @Override the Public voidOndetach () { the Super. Ondetach (); +System.out.println (TAG + ": Ondetach ()"); - } the Bayi}
Let's experiment and look at the log results:
1, start the fragment log as follows (please ignore the yellow log):
2, the State of the screen:
3. Re-light the screen:
4. Exit:
In fact, there are more cases, here is not a test. This is a simple example, but it is a good response to the callback timing of the various methods in the fragment life cycle.
Android Advanced Programming note (VI) Fragment life cycle