Original link: http://www.orlion.ga/560/
This article has actually been written in the previous article almost, but the evil of WordPress did not save! This has happened more than once!
The life cycle of a fragment
1. Fragmentation Status and callbacks
1. Operational status
The fragment is also running when a fragment is visible and the activity it is associated with is in a running state.
2. Pause Status
When an activity enters a paused state (because another activity that does not occupy the screen is added to the top of the stack), the visible fragments associated with it go into a paused state.
3. Stop state
When an activity enters a stopped state, the fragments associated with it go to the stop state. or by calling Fragmenttransaction's remove (), replace () method to remove the fragment from the activity, but with the Addtobackstack () method called before the transaction commits, the fragment will also go in into the stop state. In general, fragments entering the stop state are completely invisible to the user and may be reclaimed by the system.
4. Destruction Status
Fragments are always dependent on activity, so when the activity is destroyed, the fragments associated with it are entered into the destruction state. Or remove the fragment from the activity by calling the Remove (), replace () method of fragmenttransaction, but not before the transaction commits Addtobackst Ack () method, the fragment will also enter the destruction state.
A series of callback methods are also provided in the fragment class to cover every aspect of the fragmentation life cycle. Where there are callback methods in the activity, fragments
, but fragments also provide some additional callback methods, so let's focus on these callbacks:
1. Onattach ()
Called when the fragment is associated with an activity.
2. Oncreateview ()
Called when a view is created for fragmentation (load layout).
3. onactivitycreated ()
Make sure that the activity associated with the fragment must have been created at the time of the call.
4. Ondestroyview ()
Called when the view associated with the fragment is removed.
5. Ondetach ()
Called when fragmentation is associated with an activity.
Second, the dynamic loading layout skills
While the ability to dynamically add fragmentation is powerful enough to solve many real-world development problems, it is simply a matter of adding and replacing in a layout file. If the program can decide which layout to load depending on the resolution or screen size of the device, then we have more room to play. So in this section we'll look at the techniques for dynamically loading layouts in Android.
1. Using qualifiers
Many tablet applications are in two-page mode (the program displays a list of subkeys on the left side of the panel, displaying the content on the right panel), because the screen is large enough to display the next two pages at the same time, but the phone screen can only display one page at a time, so two pages need to be displayed separately
We need to use qualifiers (Qualifiers) to determine at run time whether the program should be using two-page mode or single-page mode. Modify the Activity_main.xml file in the Fragmentdemo project:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <fragment androi D:id= "@+id/left_fragment" android:name= "ga.orlion.fragmentdemo.LeftFragment" android:layout_width= "Match_par Ent "android:layout_height=" match_parent "android:layout_weight=" 1 "/> </LinearLayout>
Create a new Layout-large folder under the Res directory, and create a new layout under this folder, also known as Activity_main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:layout_width=" Match_parent " android:layout_height= "Match_parent" > <fragment android:id= "@+id/left_fragment" android:name= "Ga.orlion.fragmentdemo.LeftFragment" Android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1"/> <fragment android:id= "@+id/right_ Fragment " android:name=" Ga.orlion.fragmentdemo.RightFragment " &Nbsp;android:layout_width= "0DP" android:layout_height= "Match_ Parent " android:layout_weight=" 3 "/></linearlayout>
As you can see, the Layout/activity_main layout contains only one fragment, that is, single-page mode, while the Layout-large/activity_main layout contains two fragments, the two-page mode. Where large is a qualifier, those devices that are considered large by the screen will automatically load the layout under the Layout-large folder, and the layout under the Layout folder will be loaded with a small screen. Run.
Some of the most common qualifiers in Android:
2. Use the minimum width qualifier
In the previous section, we used the large qualifier to successfully solve the problem of single-page two-page judgment, but soon a new problem arose, large in the end refers to how much? Sometimes we want to be more flexible about loading layouts for different devices, whether they are identified as "large" by the system, then the minimum width qualifier (smallest-width Qualifier) can be used. The minimum width qualifier allows us to specify a minimum finger (in DP) for the width of the screen, then a device with a screen width greater than this value will load a layout, and a device with a screen width of less than this value will load another layout. Create a new LAYOUT-SW600DP folder under the Res directory, and then create a new Activity_main.xml under this folder
Layout, the code looks like this:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Match_parent" android:layout_height= "Match_parent" > <fragment android:id= "@+id/left_ Fragment " android:name=" Com.example.fragmenttest.LeftFragment " android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout _weight= "1" /> <fragment Android:id= "@+id/right_fragment" android:name= " Com.example.fragmenttest.RightFragment " android:layout_width=" 0DP " android:layout_height= "Match_parent" android:layout_weight = "3" /></LinearLayout>
This means that when the program is running on a device with a screen width greater than 600DP, the Layout-sw600dp/activity_main layout is loaded and the default layout/activity_ is still loaded when the program is running on a device with a screen width of less than 600DP Main layout. It is important to note that the minimum width qualifier was introduced in the Android 3.2 version
Getting Started with Android (vii) life cycle and qualifiers for fragments