Android-----Fragment life cycle

Source: Internet
Author: User

in the previous article we introduced the simple application of fragment and through the fragment to solve the situation of the screen switching between the display of different interfaces, through the fragment to fill the problem of extra space, this article we introduce the fragment life cycle;

As we all know, fragment can be used as a "small activity", nested in the activity, so his life cycle and activity are inseparable, Only when activity is active can the activity of the fragment be changed, the activities destroyed, and the fragment bound to him will be destroyed;

the fragment life cycle involves the following methods:

(1) Onattach (): Called when fragment is added to the activity;

(2) OnCreate (): Called when the fragment is created, the method is called once;

(3) Oncreateview (): This is a method that we often overwrite, mainly to draw the view component display problem on the fragment, the view that fragment displays is the return value of the method;

(4) onactivitycreated (): Called when the activity where the fragment is located is activated;

(5) OnStart (): Fragment when activated, similar to the activity of OnStart ();

(6) Onresume (): Call when restoring fragment, OnStart method will callback Onresume () method

(7) OnPause (): Fragment is called when paused, similar to activity onPause ();

(8) OnStop (): Called when fragment is stopped, similar to the activity of OnStop ();

(9) Ondestroyview (): Destroys the method of callback when fragment the view shown above;

(Ten) OnDestroy (): The method called when destroying the fragment, similar to the activity's OnDestroy () method;

(one) Ondetach (): When fragment and activity unbind the call, OnDestroy method will be executed after the Ondetach method, and will only be executed once;

One thing that can be noticed is that there is no Onrestart () method in the life cycle of fragment, this is also my most surprised place, I hope you know the message to me, the following example to learn the life cycle of fragment:

Define a fragment layout file: Fragment1.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical"     android:background= "#ffff00" >    <imageview        android:layout_width= "Wrap_ Content "        android:layout_height=" wrap_content "        android:src=" @drawable/fragment1 "/></linearlayout >
define the Fragment class Fragmentliferecycle and print a sentence in all of its life cycle methods:

public class Fragmentliferecycle extends fragment{@Overridepublic void onactivitycreated (Bundle savedinstancestate) { Super.onactivitycreated (savedinstancestate); System.out.println ("onactivitycreated");} @Overridepublic void Onattach (activity activity) {Super.onattach (activity); System.out.println ("Onattach");} @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); System.out.println ("OnCreate");} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) { System.out.println ("Oncreateview"); return Inflater.inflate (R.layout.fragment1, container, false);} @Overridepublic void OnDestroy () {Super.ondestroy (); System.out.println ("OnDestroy");} @Overridepublic void Ondestroyview () {Super.ondestroyview (); System.out.println ("Ondestroyview");} @Overridepublic void Ondetach () {Super.ondetach (); System.out.println ("Ondetach");} @Overridepublic void OnPause () {super.onpause (); System.out.println ("OnPause");} @Overridepublic VOID Onresume () {super.onresume (); System.out.println ("Onresume");} @Overridepublic void OnStart () {Super.onstart (); System.out.println ("OnStart");} @Overridepublic void OnStop () {super.onstop (); System.out.println ("OnStop");}}
defines the activity layout file, which is used to display the fragment, a simple layout, a button, a fragment display container Activity_fragment_liferecycle.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/button"        android:layout_width= "Wrap_content"        android:layout_height= "0px"        android:layout_weight= "1"        android:text= "show fragment"        />    <framelayout        android:id= "@+id/framelayout"        android:layout_width= "Wrap_content"        android: layout_height= "0px"        android:layout_weight= "7"        /></linearlayout>
define activity Class Fragmentliferecycleactivity

public class Fragmentliferecycleactivity extends fragmentactivity {public Button Mbutton; @Overrideprotected void OnCreate (Bundle arg0) {super.oncreate (arg0); Setcontentview (r.layout.activity_fragment_liferecycle); Mbutton = ( Button) Findviewbyid (R.id.button), Mbutton.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {fragmentliferecycle fragment = new Fragmentliferecycle (); Getsupportfragmentmanager (). BeginTransaction (). replace (r.id.framelayout,fragment). commit ();}});}}

Click "Show Fragment" button, Logcat output:

Onattach
OnCreate
Oncreateview
onactivitycreated
OnStart
Onresume

At this point, click the Home button to increase the output:

OnPause
OnStop

The discovery just paused the fragment and did not destroy it, which was similar to the activity;

Click the program icon again to enter the interface, add output:

OnStart
Onresume

You can see that the call OnStart resumed before the fragment

Press the back key to increase the output:

OnPause
OnStop
Ondestroyview
OnDestroy
Ondetach

You can see that the fragment has been destroyed and the binding to the activity has been lifted;






Android-----Fragment life cycle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.