Example to explain the fragment lifecycle control in Android application development _android

Source: Internet
Author: User
Tags static class stub

A preliminary study on the life cycle of fragment

Because the fragment must be embedded in the acitivity, the fragment lifecycle is closely related to the activity in which it resides.

If the activity is paused, all of the fragment are paused, and if the activity is stopped, all fragment in the activity cannot be started; If the activity is destroyed, Then all of its fragment will be destroyed.

However, when activity is active, it is possible to control the state of fragment independently, such as adding or removing fragment.

When this is done fragment transaction (conversion), the fragment can be placed in the back stack of the activity so that the user can perform the return operation.

When using fragment, you need to inherit fragment or fragment subclasses (Dialogfragment, Listfragment, Preferencefragment, webviewfragment), So the fragment code looks similar to the activity.

Each time you create a fragment, you first add the following three callback methods:

OnCreate (): The system calls this method when creating the fragment, where it should initialize the related components, some things that still need to be preserved even when they are paused or stopped.
Oncreateview (): This method is called when the fragment UI is first drawn, and the method returns a view, which can return NULL if fragment does not provide a UI. Note that if you inherit from Listfragment,oncreateview () The default implementation returns a ListView, so do not implement it yourself.
OnPause (): The first call to this method when the user leaves the fragment requires some changes to be submitted because the user is likely to no longer return.
There are two ways to load fragment into an activity:

Way one: Add fragment to the activity's layout file
Mode two: Dynamically add fragment in the Code of activity (recommended)
The first approach, though simple but not flexible enough. Adding fragment to the activity's layout file is equivalent to binding the fragment and its views to the view of the activity, and the fragment view cannot be switched during the life cycle of the activity.

The second approach is more complex, but it is the only way to control fragment at run time (load, remove, replace).

Ii. Life Cycle Control Examples

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 savedinstancestate) {
    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.onresume ();
  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.ondestroy ();
  LOG.E ("HJJ", "Activity &&&& OnDestroy ..."); The public static class Arraylistfragment extends Listfragment {@Override the public void Onattach (activity activit
   Y) {//TODO auto-generated Method Stub log.e ("Hjj", "arraylistfragment * * * Onattach ..."); Super.onattach (activity); @Override public void OnCreate (Bundle savedinstancestate) {//TODO auto-generated a stub log.e ("HJ
   J "," arraylistfragment * * * * * OnCreate ... ");
   Super.oncreate (savedinstancestate); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate
   {//TODO auto-generated Method Stub log.e ("Hjj", "arraylistfragment * * * Oncreateview ...");
   Return Super.oncreateview (Inflater, container, savedinstancestate); @Override public void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinst
      Ancestate);
      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 * * * Onresume ...");
    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 * * * Ondestroyview ...");
    TODO auto-generated Method Stub Super.ondestroyview (); @Override public void OnDestroy () {//TODO auto-generated Method Stub log.e ("Hjj", "Arraylistfra
     Gment * * * * * OnDestroy ... ");
    Super.ondestroy ();
     @Override public void Ondetach () {LOG.E ("Hjj", "arraylistfragment * * * * Ondetach ...");
    TODO auto-generated Method Stub Super.ondetach (); @Override public void Onlistitemclick (ListView l, View v, int position, long id) {LOG.I ("Fragmentlis
    T "," Item clicked: "+ ID);

 }
  }
}

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 (10315): Activity &&&& OnStart ...
01-22 15:30:28.123:E/HJJ (10315): arraylistfragment * * * OnStart ...

Onresume process

01-22 15:30:28.123:E/HJJ (10315): Activity &&&& onresume ...
01-22 15:30:28.123:E/HJJ (10315): arraylistfragment * * * Onresume ...

OnPause process

01-22 15:31:26.748:E/HJJ (10315): arraylistfragment * * * 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 &&&& 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): Activity &&&& onresume ...
01-22 15:31:57.537:E/HJJ (10315): arraylistfragment * * * Onresume ...

OnPause process

01-22 15:32:47.412:E/HJJ (10315): arraylistfragment * * * 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 ...

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.