Android: Life cycle of fragments

Source: Internet
Author: User

Like activities, fragmentation has its own life cycle, and it is so much like the life cycle of the activity that I believe you will soon learn, and we'll look at it in a moment.

4.3.1 Fragmentation Status and callbacks

Remember what states each activity might have in its life cycle? Yes, there are four kinds of running state, suspended state, stop state and destroy state. Similarly, each fragment may experience these states during its life cycle, but in some small places there are some differences.

1. Running state when a fragment is visible and the activity it is associated with is in a running state, the fragment is also running

State.

2. Pause Status

When an activity enters a paused state (because another activity that does not occupy the screen is added to the top of the stack), the visible fragments associated with it go into a paused state.

3. Stop state when an activity enters a stopped state, the fragments associated with it go to the stopped state. or by adjusting

The fragment is removed from the activity with the Remove (), replace () method of the Fragmenttransaction, but the Addtobackstack () method is called before the transaction commits, and the fragment goes to the stopped state. In general, fragments entering the stop state are completely invisible to the user and may be reclaimed by the system.

4. Destroying a state fragment is always dependent on the activity, so when the activity is destroyed, the fragments associated with it are entered

To the destroy state. or by calling Fragmenttransaction's remove (), replace () method to remove the fragment from the activity, but before the transaction commits, the Addtobackstack () method is not called, and the fragment goes to the destroy state. In combination with previous activities, I believe you should understand it effortlessly. In the same way, the Fragment class also provides

A series of callback methods to cover every aspect of the fragmentation life cycle. Among them, there are some callback methods in the activity, almost all fragments, but the fragment also provides some additional callback methods, we will focus on these several callbacks.

1. Onattach ()

Called when the fragment is associated with an activity.

2. Oncreateview ()

Called when a view is created for fragmentation (load layout).

3. onactivitycreated ()

Make sure that the activity associated with the fragment must have been created at the time of the call.

4. Ondestroyview ()

Called when the view associated with the fragment is removed.

5. Ondetach ()

Called when fragmentation is associated with an activity. The complete life cycle of the fragment can be referenced in Figure 4.8, which is from the Android website.

Figure 4.8

4.3.2 Experience the life cycle of fragmentation

To allow you to experience the life cycle of fragmentation more intuitively, let's practice it with an example. The example is simple and still changes based on the Fragmenttest project.

Modify the code in the Rightfragment as follows:

public class Rightfragment Extendsfragment {

public static final String TAG = "Rightfragment";

@Override

public void Onattach (activity activity) {

Super.onattach (activity); LOG.D (TAG, "Onattach");

}

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate); LOG.D (TAG, "onCreate");

}

@Override

Public View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {

LOG.D (TAG, "Oncreateview");

View view = Inflater.inflate (R.layout.right_fragment, container, false);

return view;

}

@Override

public void onactivitycreated (Bundle savedinstancestate) {

Super.onactivitycreated (savedinstancestate); LOG.D (TAG, "onactivitycreated");

}

@Override

public void OnStart () {Super.onstart (); LOG.D (TAG, "OnStart");

}

@Override

public void Onresume () {super.onresume (); LOG.D (TAG, "Onresume");

}

@Override

public void OnPause () {super.onpause (); LOG.D (TAG, "onPause");

}

@Override

public void OnStop () {super.onstop (); LOG.D (TAG, "onStop");

}

@Override

public void Ondestroyview () {Super.ondestroyview (); LOG.D (TAG, "Ondestroyview");

}

@Override

public void OnDestroy () {Super.ondestroy (); LOG.D (TAG, "OnDestroy");

}

@Override

public void Ondetach () {Super.ondetach (); LOG.D (TAG, "Ondetach");

}

}

We added the code of the print log to each callback method in Rightfragment, and then rerun the program, observing the print information in the LogCat, as shown in 4.9.

Figure 4.9

As you can see, when rightfragment is first loaded onto the screen, Onattach (), OnCreate (), Oncreateview (), onactivitycreated (), OnStart (), and Onresume () method. Then click on the button in the Leftfragment, and print information 4.10 as shown.

Figure 4.10

The OnPause (), OnStop (), and Ondestroyview () methods are executed because the anotherrightfragment replaces Rightfragment, at which point the rightfragment enters a stopped state. Of course, if the Addtobackstack () method is not called at the time of substitution, the rightfragment will go into the destruction state, and the OnDestroy () and Ondetach () methods will be executed.

Then press the back key, Rightfragment will return to the screen, print the message 4.11 shows.

Figure 4.11

The onactivitycreated (), OnStart (), and Onresume () methods are executed because the rightfragment is back in the running state. Note that the onCreate () and Oncreateview () methods do not execute at this point because we have used the Addtobackstack () method to make the rightfragment and its views not destroyed.

Press the back key again to exit the program, as shown in the print information 4.12.

Figure 4.12

The OnPause (), OnStop (), Ondestroyview (), OnDestroy (), and Ondetach () methods are executed in turn to destroy the activities and fragments together. So the complete life cycle of fragmentation you have experienced again, is not understanding more profound?

It is also worth mentioning that in the fragment you can also save the data through the Onsaveinstancestate () method, because the fragments that go into the stop state may be recycled when the system is low on memory. The saved data can be re-obtained in the three methods of OnCreate (), Oncreateview (), and onactivitycreated (), which all contain a Bundle type of savedinstancestate parameter.

Android: Life cycle of fragments

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.