The life cycle of activity and fragment

Source: Internet
Author: User

First, Activity life cycle

Second, Fragment life cycle

Three, contrast chart



Iv. Test Code

Package Com.goso.testapp;  Import android.app.Activity;  Import android.app.ListFragment;  Import Android.os.Bundle;  Import Android.util.Log;  Import Android.view.LayoutInflater;  Import Android.view.View;  Import Android.view.ViewGroup;  Import Android.widget.ArrayAdapter;    Import Android.widget.ListView;  /** * Demonstration of using listfragment to show a list of items * from a canned array. */public class Fragmentlistarray extends Activity {@Override protected void onCreate (Bundle savedinstancest          ATE) {super.oncreate (savedinstancestate);          LOG.E ("Hjj", "Activity &&&& onCreate ...");          Create the list fragment and add it as our sole content. if (Getfragmentmanager (). Findfragmentbyid (Android.              r.id.content) = = null) {arraylistfragment list = new Arraylistfragment (); Getfragmentmanager (). BeginTransaction (). Add (Android.          R.id.content, List). commit (); }} @Override Protected void OnStart () {//TODO auto-generated Method Stub Super.onstart ();      LOG.E ("Hjj", "Activity &&&& onStart ..."); } @Override protected void Onresume () {//TODO auto-generated method stub super.onresum          E ();      LOG.E ("Hjj", "Activity &&&& onresume ...");           } @Override protected void OnStop () {//TODO auto-generated Method Stub super.onstop ();      LOG.E ("Hjj", "Activity &&&& onStop ..."); } @Override protected void OnPause () {//TODO auto-generated method Stub Super.onpause (          );      LOG.E ("Hjj", "Activity &&&& onPause ..."); } @Override protected void OnDestroy () {//TODO auto-generated method stub super.ondest          Roy ();      LOG.E ("Hjj", "Activity &&&& OnDestroy ..."); } public static Class ArrayListfragment extends Listfragment {@Override public void Onattach (activity activity) {/              /TODO auto-generated Method Stub log.e ("Hjj", "arraylistfragment * * * * * * * Onattach ...");          Super.onattach (activity); } @Override public void OnCreate (Bundle savedinstancestate) {//TODO Auto-genera              Ted Method Stub log.e ("Hjj", "arraylistfragment * * * * * * OnCreate ...");          Super.oncreate (savedinstancestate);                  } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated Method Stub log.e ("Hjj", "Arraylistfra              Gment * * * * Oncreateview ... ");          Return Super.oncreateview (Inflater, container, savedinstancestate);        } @Override public void onactivitycreated (Bundle savedinstancestate) {      Super.onactivitycreated (savedinstancestate);              LOG.E ("Hjj", "arraylistfragment * * * * * * onactivitycreated ...");              string[] array = new string[]{"C + +", "JAVA", "PYTHON"}; Setlistadapter (New arrayadapter<string> (Getactivity (), Android.          R.layout.simple_list_item_1, array)); } @Override public void OnStart () {//TODO auto-generated method stub log.e              ("Hjj", "arraylistfragment * * * * * * * * OnStart ...");          Super.onstart (); } @Override public void Onresume () {LOG.E ("HJJ", "arraylistfragment * * * * * onresum              E ... ");          TODO auto-generated Method Stub super.onresume (); } @Override public void OnPause () {LOG.E ("HJJ", "arraylistfragment * * * * onPause.              ..");          TODO auto-generated Method Stub super.onpause ();               }     @Override public void OnStop () {LOG.E ("HJJ", "arraylistfragment * * * * * OnStop ...");          TODO auto-generated Method Stub super.onstop (); } @Override public void Ondestroyview () {LOG.E ("HJJ", "arraylistfragment * * * *" on              Destroyview ... ");          TODO auto-generated Method Stub Super.ondestroyview ();              } @Override public void OnDestroy () {//TODO auto-generated method stub              LOG.E ("Hjj", "arraylistfragment * * * * * * OnDestroy ...");          Super.ondestroy (); } @Override public void Ondetach () {LOG.E ("HJJ", "arraylistfragment * * * * * ONDETAC              H ... ");          TODO auto-generated Method Stub Super.ondetach ();              } @Override public void Onlistitemclick (ListView l, View v, int position, long ID) { LOG.I ("Fragmentlist", "Item clicked:" + ID);   }      }  }

V. Test results

OnCreate process 01-22 15:30:28.091:E/HJJ (10315): Activity &&&& onCreate ...  01-22 15:30:28.091:E/HJJ (10315): arraylistfragment * * * * Onattach ...  01-22 15:30:28.091:E/HJJ (10315): arraylistfragment * * * * OnCreate ...  01-22 15:30:28.115:E/HJJ (10315): arraylistfragment * * * * Oncreateview ... 01-22 15:30:28.123:E/HJJ (10315): arraylistfragment * * * * onactivitycreated ... onstart process 01-22 15:30:28.123:E/HJJ (1031  5): Activity &&&& OnStart ... 01-22 15:30:28.123:E/HJJ (10315): arraylistfragment * * * * OnStart ... onresume process 01-22 15:30:28.123:e/hjj (10315): Activ  ity &&&& Onresume ... 01-22 15:30:28.123:E/HJJ (10315): arraylistfragment * * * * Onresume ... onpause process 01-22 15:31:26.748:e/hjj (10315): Array  Listfragment * * * * OnPause ... 01-22 15:31:26.748:E/HJJ (10315): Activity &&&& onPause ... onstop process 01-22 15:31:27.638:e/hjj (10315):  Arraylistfragment * * * * OnStop ... 01-22 15:31:27.638:E/HJJ (10315): Activity &&&&amP  OnStop OnStart Process 01-22 15:31:57.537:e/hjj (10315): Activity &&&& OnStart ... 01-22 15:31:57.537:E/HJJ (10315): arraylistfragment * * * * OnStart ... onresume process 01-22 15:31:57.537:e/hjj (10315): Activ  ity &&&& Onresume ... 01-22 15:31:57.537:E/HJJ (10315): arraylistfragment * * * * Onresume ... onpause process 01-22 15:32:47.412:e/hjj (10315): Array  Listfragment * * * * OnPause ... 01-22 15:32:47.412:E/HJJ (10315): Activity &&&& onPause ... onstop process 01-22 15:32:47.865:e/hjj (10315):  Arraylistfragment * * * * OnStop ... 01-22 15:32:47.865:E/HJJ (10315): Activity &&&& onStop ... ondestroy process 01-22 15:32:47.865:e/hjj (10315)  : Arraylistfragment * * * * Ondestroyview ...  01-22 15:32:47.865:E/HJJ (10315): arraylistfragment * * * * OnDestroy ...  01-22 15:32:47.865:E/HJJ (10315): arraylistfragment * * * * Ondetach ...   01-22 15:32:47.865:E/HJJ (10315): Activity &&&& OnDestroy ...

  

The life cycle of activity and fragment

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.