Everything Fragment should know (1)

1. What is Fragment?

    Fragment means "fragment, fragment ". Fragments are parts of an application's user interface or behavior that can be placed in an Activity, enabling the application to provide a layout that scales between large and small-screen devices.

2, the reason

    The first class you learn to use when you become an Android developer is the Activity class. After all, the Activity class provides a user interface for the application. By organizing user interface components in an activity, the activity becomes a canvas for drawing your application masterpiece. In the early days of Android, building an app's user interface directly in an activity worked well. Most of the early applications had relatively simple user interfaces, and the number of different Android device form factors was small. In most cases, a single activity can span different device shapes with a few layout resources.

    Later, Android devices came in a variety of form factors, with incredible variations in size and shape. When combined with the highly interactive user interfaces of modern Android applications, it becomes very difficult to create a single activity to efficiently manage user phone interfaces across these different form factors. A possible solution is to define an Activity that provides the user experience for a subset of the device form factor - such as a smartphone; then, another Activity can be defined for a different subset of form factors , such as a tablet. The problem with this approach is that activities tend to have a lot of business logic beyond simply presenting the user interface . Because multiple activities perform essentially the same tasks, the logic within each activity must be duplicated, or the program complexity must be increased by figuring out ways to share logic between activities (such as creating potentially complex inheritance relationships). The approach of using different activities for different form factors also greatly increases the number of activities in the program , achieving double or triple the number of activities required. Additionally, the advent of Google's Material Design specification has further increased the complexity of the code contained within each Activity . Therefore, a better solution is needed, which can modularize the user interface of the application, and can be used in the Activity as neededAdd the needed part in ; Fragment is the solution. Fragments allow us to divide the user interface into functional groups of user interface components and logic. An Activity can load and arrange Fragments as needed for a given device shape. Fragment takes care of form factor details, while Activity manages the entire user interface issue. Fragments can also play an important role in grouping user interface components in a way that simplifies your Material Design application.

3. Features of Fragment

    Fragment was introduced into the program in API Level 11 (Android 3.0), and today, more than 95% of android devices support Fragment.

    The relationship between Fragment and Activity:

    The Fragment class can use many methods to achieve various results. At its core, it represents a specific operation or interface that runs within a larger Activity. Fragment is closely related to the Activity in which it is located and cannot be used alone. Although Fragment defines its own lifecycle, the lifecycle is dependent on its activity: if the activity stops, no fragments inside it can start; when the activity is destroyed, all fragments are destroyed.

    All subclasses of Fragment must contain a public parameterless constructor. Frameworks will often re-instantiate a fragment class when needed, especially during state restoration, and need to be able to find this constructor to instantiate it. A runtime exception occurs during state restoration if the parameterless constructor is not available.

4. Simple to use

    Requirement: Use Fragment to handle the logical function of the setting interface

    1. Create interface layout fragment_setting.xml

    

    2. Create SettingFragment

    To create a SettingFragment, you must inherit Fragment. At this time, you will be prompted that the import package includes android.suppout.app.Fragment and android.suppout.v4.app.Fragment. At this time, you need to import the Fragment of the v4 package for compatibility. Otherwise, in some versions, the cause a crash.

        

        

        3. Load layout resources for SettingFragment

     Fragment loads layout resources and must override the onCreateView() method of Fragment to return View. As shown in the figure:        

            Since the onCreateView() method needs to return a View, load it into the layout resource through the layout filler to generate the View and return it.

        4. Load Fragment to Activity

            In the original project, create a new CommonFragmentActivity, load SettingFragment into CommonFragmentActivity, and add a button to enter CommonFragmentActivity in MainActivity, click to enter.

               1. Create CommonFragmentActivity and layout activity_common_fragment.xml

                

                In activity_common_fragment.xml, enter SettingFragment through the tag fragment, and set the unique identifier id. This way is through static loading of layout resources.

                

                5. Enter CommonActivity

                The jump of Activity is no longer detailed, and I don't know everything that Activity should know (1) , the effect is as shown in the figure:

                  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325439238&siteId=291194637