I. Fragment life cycle Diagram
two. Introduction to the Fragment life cycle approach
The life cycle of the fragment is similar to the activity life cycle, and its lifecycle approach is as follows.
Onattach: |
Bind to activity (called when fragment and activity are associated). |
OnCreate: |
Create fragment |
Oncreateview: |
Creates a fragment layout (called when the layout is loaded for fragment). |
Onactivitycreated: |
After the activity is created (called when the OnCreate method in the activity finishes executing). |
OnStart: |
Visible, non-interactive |
Onresume: |
Visible, can be interactively |
OnPause: |
Partially visible, non-interactive |
OnStop: |
Not visible |
Ondestroyview: |
Destroys the Fragment View object (called when the layout in fragment is removed). |
OnDestroy: |
Fragment destroyed the |
Ondetach: |
is unbound from activity (called when fragment and activity disassociate). |
The following content is from http://www.cnblogs.com/purediy/p/3276545.html
1. When a fragment is created, it goes through the following States.
- Onattach ()
- OnCreate ()
- Oncreateview ()
- Onactivitycreated ()
2. When the fragment is visible to the user, it will experience the following States.
3. When this fragment enters "background mode", it will experience the following status.
4. When the fragment is destroyed (or the activity that holds it is destroyed), it goes through the following States.
- OnPause ()
- OnStop ()
- Ondestroyview ()
- OnDestroy ()//Originally omitted class this callback, thanks to xiangxue336 proposed.
- Ondetach ()
5. Just like Activitie, in the following States, you can use the bundle object to save an fragment object.
- OnCreate ()
- Oncreateview ()
- Onactivitycreated ()
6. Most of the state of fragments is similar to Activitie, but fragment has some new states.
- Onattached ()--called when fragment is added to the activity (the activity is available in this method).
- Oncreateview ()-When the activity is going to get fragment layout, call this method, where fragment creates its own layout (interface).
- Onactivitycreated ()--Call this method when the activity's oncreated () method returns
- Ondestroyview ()-this method is called when the view in the fragment is removed.
- Ondetach ()--this method is called when fragment and activity are separated.
Once the activity enters the resumed state (that is, the running state), you are free to add and remove fragment. Therefore, the life cycle of fragment can be operated independently only when the activity is in the resumed state, and other times depend on the activity's life cycle change.
Android Fragment learning note ② (Fragment life cycle)