Introduction to Android (VII) life cycle and qualifier of fragments

Original link: http://www.orlion.ga/560/

This article is almost as written in the last article, but the evil WordPress has not been saved! This has happened more than once!

1、 Life cycle of debris

1. Status and callback of fragments

1. Operation status

When a fragment is visible and its associated activity is running, the fragment is also running.

2. Suspended status

When an activity enters the suspended state (because another activity that does not occupy the full screen is added to the top of the stack), the visible fragment associated with it enters the suspended state.

3. Stop status

When an activity enters the stop state, the fragments associated with it will enter the stop state. Or by calling FragmentTransaction's remove (), replace () method to remove fragments from the activity, but the addToBackStack () method is called before the transaction is committed, and the fragments will also enter the stop state. In general, the fragments entering the stop state are completely invisible to the user and may be recycled by the system.

4. Destruction status

Fragments always exist attached to the activity, so when the activity is destroyed, the fragments associated with it will enter the destruction state. Alternatively, the fragments can be removed from the activity by calling the remove() and replace() methods of the fragmenttransaction, but the addtobackstack() method is not called before the transaction is committed. At this time, the fragments will also enter the destruction state.

The fragment class also provides a series of callback methods to cover each link of the fragment life cycle. Among them, there are callback methods and fragments in the activity

There are almost all callbacks in, but the fragment also provides some additional callback methods. Let's focus on these callbacks:

1. onAttach()

Called when the fragment is associated with the activity.

2. onCreateView()

Called when creating a view (loading a layout) for the fragment.

3. onActivityCreated()

Ensure that the activity associated with the fragment must be called when it has been created.

4. onDestroyView()

Called when the view associated with the fragment is removed.

5. onDetach()

Called when the fragment is disassociated from the activity.

2、 Skills of dynamically loading layout

Although the function of dynamically adding fragments is very powerful and can solve many problems in practical development, it is only adding and replacing in a layout file after all. If the program can decide which layout to load at runtime according to the device resolution or screen size, we have more space to play. Therefore, in this section, we will discuss the skills of dynamically loading layout in Android.

1. Use qualifier

Many tablet applications adopt the two page mode (the program displays a list containing sub items on the left panel and the content on the right panel), because the screen on the tablet is large enough to display the contents of the next two pages at the same time, but the mobile phone screen can only display the contents of one page at a time, so the two pages need to be displayed separately

We need "qualifiers" to determine whether the program should use two page mode or single page mode at run time. Modify the activity in the fragmentdemo project_ Main.xml file:

<?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="match_parent"
        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"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>
</LinearLayout>

As you can see, layout / activity_ The main layout contains only one fragment, that is, single page mode, and layout large / activity_ The main layout contains two fragments, the two page mode. Large is a qualifier. Devices whose screens are considered to be large will automatically load the layout under the layout large folder, while those with small screens will load the layout under the layout folder. function.

Some common qualifiers in Android:

2. Use minimum width qualifier

In the previous section, we successfully solved the problem of judging one page and two pages by using the large qualifier, but soon a new problem appeared. How big does large mean? Sometimes we want to be more flexible in loading layouts for different devices, whether they are recognized as "large" by the system or not. At this time, we can use the minimum width qualifier. The minimum width qualifier allows us to specify a minimum finger (in DP) for the screen width, and then take this minimum value as the critical point. Devices with a screen width greater than this value load one layout, and devices with a screen width less than this value load another layout. Create a new layout-sw600dp folder in the res directory, and then create an activity in this folder_ main.xml

The layout code is as follows:

<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 runs on a device with a screen width greater than 600dp, layout-sw600dp / activity will be loaded_ Main layout. When the program runs on a device with a screen width of less than 600dp, the default layout / activity will still be loaded_ Main layout. It should be noted that the minimum width qualifier was introduced in Android version 3.2

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>