Simple use summary of Android development--fragment

Source: Internet
Author: User

Objective:

Before the project, the use of this fragment, in the middle encountered a lot of pits, the pits are solved, and now write a simpler fragment use summary

A brief introduction to fragment:

Simply put, fragment can actually be understood as a control with its own life cycle, except that the control is a bit special, it has its own ability to handle input events, has its own life cycle, and must rely on activity to communicate and host with each other.

Using fragment can streamline the activity file's code, facilitate better reading, and at the same time, have a better speed to load

Another case of using fragment is that you can display different interfaces on your tablet and phone, which is not explained by the lack of contact with the development of the tablet app.

Fragment use: Table of Contents: 1. Create fragment and fragment corresponding layout files 2. Use static methods to create fragment (used in XML files) 3. Dynamically Create and use fragment (Java code) 4. By Fragmentmanager method to find Fragment instances created by static methods5.Fragment is used like activity6.Fragment Lifecycle 7.Fragment Message Delivery (FOCUS) 8.Fragment Click event Conflict Handling

1. Create fragment and fragment corresponding layout files

Created we can manually create a fragment file and an XML layout file, and then call inflate in the Oncreateview method of fragment to return a view

In fact, Android Studio gives us a very good automated generation fragment and layout file tool, we directly new, choose Fragment->blank can

As for the other several fragment, are interested in Baidu to find information

After we came to the new interface, Android Studio automatically generated the layout for fragment and fragment, and the code also added the previous line of code to refresh the view.

To facilitate the use of the following, we simply set the background of this layout file fragment__test to red

2. Creating fragment using static methods (used in XML files)
<Fragment
android:id="@+id/fragmetn1"
Android:layout_width="Match_parent"
android:layout_height="Match_parent"
Android:name="Com.wan.fragmenttest.TestFragment"
tools:layout="@layout/fragment_test"/>

The code above is to add fragment code, and we use the control is similar to the need to use the name of the property to specify the relevant fragment

ID is required, no words will appear flash back, tools:layout is used to display the current fragment, no, the preview is gray, can not be used, but convenient to see the effect or use it

We're doing this successfully. Use a fragment with static methods

3. Dynamically create and use fragment (Java code)

First, we delete the previous fragment in the activity layout file, replace it with a framelayout, and define an ID for it.

Later, we new a fragment, using the fragmenttransaction add or replace method, put it into a framelayout

Fragmenttransaction obtained by Fragmentmanager's BeginTransaction method

The first parameter of the Add method is a layout , which is the ID of our previous framelayout, and the second is fragment, a testfragment of our previous new, Commit commits the transaction, does not commit the transaction is invalid, does not believe the word can self-test

PS: Note Here that fragment has two different packages, one is app and the other is V4

As can be seen from the figure, what is the difference between these two?

The Android.support.v4.app.fragment package is for backwards compatibility to 1.6 systems.

android.app.fragment Package is the 3.0 system after the ability to use, but it does not matter, now android4.0 has occupied 99% of the market

we use the app as a package.

If you are using a V4 package, you need to use the Getsupportfragmentmanager method to get Fragmentmanager

If you are using an app package, use the Getfragmentmanager method directly to get Fragmentmanager

Dynamic we can only run to view test results and see the same interface as before using static methods

4. Find the fragment instance created by the static method by Fragmentmanager method

Two methods:

Findfragmentbyid method and Findfragmentbytag

We define an ID in XML, and then we can find the instance through the Findfragmentbyid method

In the same way, we define a tag for fragment in XML, and then we can find the instance by the Findfragmentbytag method.

There are two ways to use fragment we can pass fragment

5.Fragment is used like activity

If we use a button and other controls in our fragment, we can find an instance of the control, and set the listener for it, like in activity, through the Findviewbyid method

However, it is important to note that fragment can find an instance of the control through Findviewbyid only after the view has been created, that is, the Findviewbyid method can only be used in onviewcreated

Life cycle of 6.Fragment

Fragment 1. Interface Open OnCreate () Method Execution!                 Oncreateview () Method Execution!       Onactivitycreated () Method Execution!    OnStart () Method Execution! Onresume () Method Execution! 2. Press the main screen key/lock screen OnPause () Method Execution! OnStop () Method Execution!3. Re-openOnStart () Method Execution! Onresume () Method Execution!4. Press the Back buttonOnPause () Method Execution!    OnStop () Method Execution!    Ondestroyview () Method Execution!    OnDestroy () Method Execution! Ondetach () Method Execution!adding fragment to the activity, the corresponding life cycle1. OpenFragment onCreate () method Execution! Fragment Oncreateview () method Execution! Activity onCreate () method Execution! Fragment onactivitycreated () method Execution! Activity OnStart () method Execution! Fragment OnStart () method Execution! Activity Onresume () method Execution! Fragment Onresume () method Execution!2. Press the main screen key/lock screenFragment onPause () method Execution! Activity onPause () method Execution! Fragment onStop () method Execution! Activity onStop () method Execution!3. Open againActivity Onrestart () method Execution! Activity OnStart () method Execution! Fragment OnStart () method Execution! Activity Onresume () method Execution! Fragment Onresume () method Execution!4. Press the Back buttonFragment onPause () method Execution! Activity onPause () method Execution! Fragment onStop () method Execution! Activity onStop () method Execution! Fragment Ondestroyview () method Execution! Fragment OnDestroy () method Execution! Fragment Ondetach () method Execution! Activity OnDestroy () method Execution!
Message delivery in 7.Fragment

In activity, we use bundles to carry data for delivery, and in fragment, bundles that are used

How to use it?

First, create a static method for fragment Newinstance

We can enter newinstance directly, there will be a code completion prompt, the selection will be complete after the code

method as shown above, first, create a bundle of objects and a Testfragment object, call fragment in the setarguments, the bundle is stored in, and then return a testfragment

The bundle here is not carrying any data, and now we want to pass in a string of data, as shown in the code below

So we're done passing the data, but, in one step, we have to receive the data, from the previous life cycle, we can know that the OnCreate method is the first step, so, in the OnCreate method to receive the data from the bundle

Before receiving the data, we have to declare several global variables in the fragment file to receive the data

This completes the delivery of the message.

Supplemental reference: Data transfer and refresh UI in Android development--fragment (change control)

8.Fragment Click event Conflict Handling

In the picture, there are two diaries, and a click will pop up to view the diary

As can be seen from the diagram, our clicks through the current fragment, which is obviously not the result we want, what should we do? The answer is simply to add a Ontouch listener to the currently displayed fragment

PS: The context can be obtained by getactivity method in fragment

Simple use summary of Android development--fragment

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.