Android development-Summary of simple use of Fragment, androidfragment

Source: Internet
Author: User

Android development-Summary of simple use of Fragment, androidfragment
Preface:

This Fragment was used in the previous project, and many pitfalls were encountered in the middle to solve the pitfalls. Now I am writing a simple summary of Fragment usage.

Fragment:

In short, Fragment can be understood as a control with its own lifecycle, but it is a bit special. It has its own ability to process input events and its own lifecycle, it must be dependent on Activity and can communicate with each other and be managed.

Fragment can simplify the code of the Activity file for better reading and better loading speed.

Another use case of Fragment is that different interfaces can be displayed on the tablet and mobile phone. This is not explained because the tablet APP is not developed.

Fragment usage: Directory: 1. create a layout file for Fragment and Fragment 2. use static methods to create Fragment (used in xml files) 3. dynamically create and use Fragment (java code) 4. use the FragmentManager method to find the Fragment instance created by the static method 5. fragment uses 6 like activity. fragment lifecycle 7. fragment message transmission (emphasis) 8. fragment click event conflict handling

 

1. Create layout files for Fragment and Fragment

If you create a Fragment file and an xml layout file, call inflate in the onCreateView method of Fragment to return a View.

In fact, Android Studio provides us with a good tool for Automatically Generating Fragment and layout files. We can choose Fragment> blank for new tools.

For other Fragment types, if you are interested, contact Baidu for information.

Then we came to the newly created interface. Android Studio will automatically generate the layout corresponding to Fragment and Fragment, and add the previous line of code to the Code to refresh the view.

To facilitate the following usage, we simply set the background of the layout File fragment _ test to red.

2. Use static methods to create Fragment (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 above code is the code for adding Fragment, which is similar to the Code for using the control. You need to use the name attribute to specify the relevant Fragment.

Id is required. If not, a crash will occur. tools: layout is used to display the current Fragment. If not, the preview is gray and usable, however, it is easy to view the effect.

 

In this way, a Fragment is successfully used in the static method.

3. dynamically create and use Fragment (java code)

First, we delete the Fragment in the previous activity layout file, change it to a framelayout, and define an id for it.

 

Then, we create a new Fragment and use the add or replace method of FragmentTransaction to put it into a framelayout.

FragmentTransaction is obtained by the beginTransaction method of FragmentManager.

The first parameter of the add method is a layout, which is our previous framelayout id, and the second parameter is fragment. We used a new TestFragment and commit to commit transactions, it is invalid to not commit the transaction. If you do not believe it, you can test it on your own.

 

PS: note that Fragment has two different packages: app and v4.

As shown in the figure, what are the differences between the two?

The android. support. v4.app. fragment package is intended to be backward compatible with the 1.6 system.

The android. app. fragment package can be used only after the 3.0 system, but it doesn't matter. Now android4.0 occupies 99% of the market

We can use the app package.

If you are using a v4 package, use the getSupportFragmentManager method to obtain the FragmentManager.

If the app package is used, use the getFragmentManager method to obtain the FragmentManager.

Dynamic, we can only run to view the test results. We can see that the interface is the same as the previous static method.

4. Use the FragmentManager method to find the Fragment instance created by the static method.

Two methods:

The findFragmentById method and findFragmentByTag

We define an id in xml, and then we can find the instance through the findFragmentById method.

Similarly, we define a Tag for Fragment in xml, and then we can find the instance through the findFragmentByTag method.

Fragment can be used in two ways: Fragment

 

5. Fragment is used like activity

If buttons and other controls are used in Fragment, we can find the control instance through the findViewbyid method in activity and set the listener for the control.

However, it should be noted that Fragment can only find the control instance through findviewbyid after the View is created, that is, the findviewbyid method can be used only in onViewCreated.

 

 

6. Fragment Lifecycle

 

Fragment1. Open the interfaceOnCreate () method execution! OnCreateView () method execution! OnActivityCreated () method execution! OnStart () method execution! OnResume () method execution!2. Press the home screen key/lock screenOnPause () method execution! OnStop () method execution!3. re-openOnStart () method execution! OnResume () method execution!4. Press the back key.OnPause () method execution! OnStop () method execution! OnDestroyView () method execution! OnDestroy () method execution! OnDetach () method execution!Add Fragment to Activity, corresponding Lifecycle1. 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 home screen key/lock the Fragment onPause () method! Activity onPause () method execution! Fragment onStop () method execution! Activity onStop () method execution! 3. Open Activity onRestart () method again and execute it! Activity onStart () method execution! Fragment onStart () method execution! Activity onResume () method execution! Fragment onResume () method execution! 4. Press the back key to execute the Fragment onPause () method! 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!
7. Fragment message transmission

In activity, we use Bundle to carry data for transmission. In Fragment, Bundle is also used.

How to use it?

First, create a static method newInstance for Fragment.

We can directly enter newInstance, and the Code Completion prompt will appear. After selection, the code will be completed.

As shown above, a Bundle object and a TestFragment object are created, and setArguments in Fragment is called to store the bundle. Then, a TestFragment is returned.

The bundle here does not carry any data. Now we want to input a String of data, the Code is as follows:

In this way, we have completed the data transfer. However, we have to receive the data. From the previous lifecycle, we can know that the onCreate method is the first step. Therefore, receive data from Bundle in onCreate Method

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

This completes message transmission.

Additional reference: Android development-data transfer and refresh UI in fragment (Change controls)

8. Fragment click event conflict handling

As shown in the figure, there are two diaries. Click to view the diaries.

As shown in the figure, we click through the current Fragment. This is obviously not the expected result. What should we do? The answer is simple: add an onTouch listener to the currently displayed Fragment.

 

PS: In Fragment, you can use the getActivity method to obtain the context

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.