Android's fragment lifecycle states and callback functions use _android

Source: Internet
Author: User

callback function

Like activities, fragments has their own life cycle. Understanding the lifecycle of fragments allows you to save instances of them when they are destroyed so that they can revert to their previous state when they are created again.

Process:
Onattach ()
role: Fragment has been linked to activity,

This is the callback function.

  @Override public
  void Onattach [activity activity] {
      Super.onattach (activity);
      LOG.I ("Onattach_fragment");
  }

At this time the activity has been passed in, and the value of the transfer of the activity can be carried out in communication with the activity, or getactivity (), provided that the fragment is already associated with the host's activity and is not detached from , he only calls once.
OnCreate ()
when the system creates the fragment, it recalls him and instantiates some of the variables inside him.
These variables are mainly: The data you want to keep when you pause to stop
If we're going to start a background thread for fragment, consider putting the code here.
Parameters are: Bundle savedinstance, for saving Fragment parameters, Fragement can also rewrite Onsaveinstancestate (bundleoutstate) method, save Fragement State;
Can be used for file Protection
He only called once.

Oncreateview ()

The first time you use it, fragment will draw a layout out of it, in order to draw the control to return a layout view, or you can return null.

When the system is used to fragment fragment will return to his view, the faster the better, so try not to do time-consuming operations here, such as loading a large number of data from the database display ListView,
Of course the thread is still OK.

To draw the UI layout for the current fragment, you can use the thread to update the UI, which is to load the fragment layout.
This is generally the first to determine whether or not NULL.

if (text==null) {
      Bundle args=getarguments ();
      Text=args.getstring ("text");
    }
    if (view = = null) {
      view = inflater.inflate (R.layout.hello, null);
    }

This makes each judgment so that it doesn't have to be loaded each time, reducing resource consumption

Onactivitycreated ()

Called when the OnCreate method in the activity has finished executing.

Note:
As you can see from this official remark: when executing onactivitycreated (), the activity
OnCreate just finished.
So the oncreate of the activity before the onactivitycreated () call may not have been completed,
Therefore, UI operations that interact with the activity cannot be oncreateview (), and UI interaction can be performed in onactivitycreated ().
So: This approach is primarily to initialize the UI that you need for your parent activity or fragment to be finished
The element that the integer initialization can initialize.
If the oncreateview inside the initialization of space will be much slower, such as ListView.
OnStart ()

Consistent with the activity, initiates a callback when Fragement starts, at which point the fragement is visible.
Onresume ()

Consistent with the activity is visible in the activity. Active, fragement into the foreground, and can be activated when the focus is obtained.
OnPause ()

And the activity is consistent with other activity to gain focus and this is still visible
On the first call, it means that the user left the fragment (not destroyed)
Typically used for user submissions (may not come back after the user leaves)
onStop ()

Consistent with activity, fragment invisible, possible situations: activity was stopped or fragment removed but was, added to the fallback stack, a stopped fragment is still alive if long time does not have to be removed.
Ondestroyview ()

Called when the layout in fragment is removed.
Represents the UI layout associated with fragemnt destruction, and clears all resources associated with the view.

I used to think there was no use here, actually great article can do it,
I believe we all used viewpager+fragment, because the Viewpager caching mechanism, each time will load 3
Page.
For example: There are four fragment when sliding to page fourth the first page executes Ondestroyview (), but no
Perform OnDestroy. He is still associated with the activity. When you slide to the first page, you perform the
Oncreateview (). Life cycle You can try it yourself.
So here's the problem. There will be repeated load view, so do it (below is the code of the Ancestors)

@Override public
  void Ondestroyview () {
    log.i ("ondestroyview_fragment");
    if (view!=null) {
            (ViewGroup) view.getparent ()). Removeview (view);
    Super.ondestroyview ();
  }

OnDestroy ()

Destroy the Fragment object, similar to the activity.
Ondetach ()

Fragment is invoked when the activity is released from the association. Out of the activity.
Visible fragment destruction is still very elegant, one of the come.

Here's the lifecycle of activity and fragment at the same time

Start Start:

05-07 05:55:08.553:i/log (1990): OnCreate
05-07 05:55:08.553:i/log (1990): Onattach_fragment
05-07 05:55:08.553:i/log (1990): Oncreate_fragment
05-07 05:55:08.553:i/log (1990): Oncreateview_fragment
05-07 05:55:08.553:i/log (1990): Onactivitycreated_fragment
05-07 05:55:08.553:i/log (1990): OnStart
05-07 05:55:08.553:i/log (1990): Onstart_fragment
05-07 05:55:08.553:i/log (1990): Onresume
05-07 Log (1990): Onresume_fragment

Press the Home button

05-07 05:55:28.725:i/log (1990): Onpause_fragment
05-07 05:55:28.725:i/log (1990): OnPause
05-07 05:55:29.221 : I/log (1990): Onstop_fragment
05-07 05:55:29.221:i/log (1990): OnStop

And then back to the interface

05-07 05:55:49.441:i/log (1990): Onrestart
05-07 05:55:49.441:i/log (1990): OnStart 05-07
( 1990): Onstart_fragment
05-07 05:55:49.441:i/log (1990): Onresume 05-07 (05:55:49.441:i/log): 1990
Fragment

Destruction of activity

05-07 05:59:02.293:i/log (1990): Onpause_fragment
05-07 05:59:02.293:i/log (1990): OnPause
05-07 05:59:02.757 : I/log (1990): Onstop_fragment
05-07 05:59:02.757:i/log (1990): OnStop 05-07
(1990): Ondestroyview_fragment
05-07 05:59:02.757:i/log (1990): Ondestroy_fragment 05-07
, 05:59:02.757:i/log (1990): Ondetach_fragment
05-07 05:59:02.757:i/log (1990): OnDestroy

It can be seen that when the real fragment the activity method first, when the destruction is the implementation of the fragment method, so that a better understanding of fragment is nested in the activity

The following comprehensive example tests the different states of the fragments:
1. Create a fragment subclass: Fragment1.java.

Package net.horsttnann.Fragments; 
Import android.app.Activity; 
Import android.app.Fragment; 
Import Android.os.Bundle; 
Import Android.util.Log; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
 
Import Android.view.ViewGroup; public class Fragment1 extends Fragment {@Override public View oncreateview (layoutinflater inflater, ViewGroup cont 
 
    Ainer, Bundle savedinstancestate) {log.d ("Fragment 1", "Oncreateview"); 
  ---Inflate the layout for this fragment---return inflater.inflate (R.layout.fragment1, container, false); 
    @Override public void Onattach (activity activity) {Super.onattach); 
  LOG.D ("Fragment 1", "Onattach"); 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  LOG.D ("Fragment 1", "onCreate"); @Override public void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstances 
  Tate);  LOG.D ("Fragment 1", "onactivitycreated"); 
    @Override public void OnStart () {Super.onstart (); 
  LOG.D ("Fragment 1", "OnStart"); 
    @Override public void Onresume () {super.onresume (); 
  LOG.D ("Fragment 1", "Onresume"); 
    @Override public void OnPause () {super.onpause (); 
  LOG.D ("Fragment 1", "OnPause"); 
    @Override public void OnStop () {super.onstop (); 
  LOG.D ("Fragment 1", "onStop"); 
    @Override public void Ondestroyview () {Super.ondestroyview (); 
  LOG.D ("Fragment 1", "Ondestroyview"); 
    @Override public void OnDestroy () {Super.ondestroy (); 
  LOG.D ("Fragment 1", "OnDestroy"); 
    @Override public void Ondetach () {Super.ondetach (); 
  LOG.D ("Fragment 1", "Ondetach"); 
 } 
}

2. Press CTRL+F11 to convert the simulator to "Horizontal screen mode".
3. Press F11 to Debug.
4. When the application is loaded, the Logcat window has the following display.

03-27 12:23:32.255:d/fragment 1 (704): Onattach 
03-27 12:23:32.255:d/fragment 1 (704): OnCreate 
03-27 12:23:32.255:d/fragment 1 (704): Oncreateview 
03-27 12:23:32.274:d/fragment 1 (704): onactivitycreated 
03-27 12:23:32.274:d/fragment 1 (704): OnStart 
03-27 12:23:32.286:d/fragment 1 (704): Onresume 

5. Press the Home key, the Logcat window has the following display.

03-27 12:25:23.174:d/fragment 1 (704): OnPause 
03-27 12:25:25.174:d/fragment 1 (704): OnStop 

6. Press home key not to put, re-enter the application, the Logcat window has the following display.

03-27 12:26:21.505:d/fragment 1 (704): OnStart 
03-27 12:26:21.505:d/fragment 1 (704): Onresume 

7. Press the return key, the Logcat window has the following display.

 03-27 12:27:54.384:d/fragment 1 (704): OnPause 03-27 12:27:55.324:d/fragment 1 (704): OnStop 03-27 12:27:55.324:d/fragment 1 (704): Ondestroyview 03-27 12:27:55.324:d/fragment 1 (704): OnDestroy 03-27 12:2 7:55.324:d/fragment 1 (704): Ondetach 

Analytical:
1. When a fragment is created, it experiences the following States.
Onattach ()
OnCreate ()
Oncreateview ()
Onactivitycreated ()
2. When this fragment is visible to the user, it will experience the following state.
OnStart ()
Onresume ()
3. When this fragment enters "backstage mode", it will experience the following state.
OnPause ()
OnStop ()
4. When the fragment is destroyed (or the activity that holds it is destroyed), it experiences the following States.
OnPause ()
OnStop ()
Ondestroyview ()
Ondetach ()
5. Just like Activitie, in the following States, you can use the bundle object to save a fragment object.
OnCreate ()
Oncreateview ()
Onactivitycreated ()
Most of the 6.fragments states are similar to Activitie, but fragment has some new states.
Onattached ()--this method is invoked when the fragment is associated with an activity.
Oncreateview ()--when creating a view in fragment, call this method.
Onactivitycreated ()-This method is invoked when the OnCreate () method of the activity is returned.
Ondestroyview ()-This method is invoked when the view in fragment is removed.
Ondetach ()-Call this method when fragment and activity are separated.

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.