Android Learning Note (15)-The life cycle of fragments (with source)

Source: Internet
Author: User

The life cycle of fragments


Click to download the source code


Like activities, fragments have their own life cycle. After understanding the life cycle of the fragment, we can correctly save the example when the fragment is destroyed and restore it to the previous state when the fragment is rebuilt.


1, use the previous article fragments, add the following code in the Fragment1.java file:

Package Net.zenail.fragments;import Android.app.activity;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class Fragment1 extends Fragment {@Overridepublic view Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated method STUBLOG.D ("Fragment 1", "Oncreateview");//Instantiate Layout file return Inflater.inflate (R.layout.fragment1, container, false);} @Overridepublic void Onattach (activity activity) {//TODO auto-generated method Stubsuper.onattach (activity); LOG.D ("Fragment 1", "Onattach");} @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); LOG.D ("Fragment 1", "onCreate");} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.onactivitycreated (savedinstancestate); LOG.D ("Fragment 1", "onactiVitycreated ");} @Overridepublic void OnStart () {//TODO auto-generated method Stubsuper.onstart (); LOG.D ("Fragment 1", "OnStart");} @Overridepublic void Onresume () {//TODO auto-generated method Stubsuper.onresume (); LOG.D ("Fragment 1", "Onresume");} @Overridepublic void OnPause () {super.onpause (); LOG.D ("Fragment 1", "OnPause");}; public void OnStop () {//TODO auto-generated method Stubsuper.onstop (); LOG.D ("Fragment 1", "OnStop");} @Overridepublic void Ondestroyview () {//TODO auto-generated method Stubsuper.ondestroyview (); LOG.D ("Fragment 1", "Ondestroyview");} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); LOG.D ("Fragment 1", "OnDestroy");} @Overridepublic void Ondetach () {//TODO auto-generated method Stubsuper.ondetach (); LOG.D ("Fragment 1", "Ondetach");}}

2, press CTRL+F11, the Android simulator to switch to landscape mode;

3. Press the F11 key in Eclipse to debug the application on the emulator;

4. When the application is loaded into the emulator, the Logcat window will display the following:


5. Click the Home button on the emulator to display the following output in the Logcat window:


6. Press and hold the home button on the simulator, then click Fragments to launch the application, Logcat display as follows:


7. Finally, click the Back button in the emulator and the Logcat window displays the following output:


8, from the above example, we know that the process of debris experience is as follows:

When fragments are created: Onattach ()-->oncreate ()-->oncreateview ()-->onactivitycreated ();

When the fragment enters background mode: OnPause ()-->onstop ();

When the fragment becomes visible: OnStart ()-->onresume ();

When the fragment is destroyed: OnPause ()-->onstop ()-->ondestroyview ()-->ondestroy ()-->ondetach ();

9. As with activities, fragments can use bundle objects to restore instances of fragmentation in the following states:

OnCreate (), Oncreateview (), onactivitycreated ().

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.