The use of Android fragment
1. Use Support Library
If your application needs to run in versions 3.0 and above, you can ignore this part of the content.
If your application uses a version below 3.0, 1.6, or more, you need to use a support library to build it.
To use the Support library:
1. Use the SDK Manager tool under the SDK to download the Android Support Package
2. Create a libs directory under the top-level directory of your Android project
3. Locate the/extras/android/support/v4/android-support-v4.jar under your SDK and copy it to the Libs of your project, select this jar package → right button →build path→add to build P Ath
4. Add under the label of your project's Manifest.xml file:
android:targetsdkversion= "8"/>
Where targetsdkversion is the least supported version of your software
5. If your project supports the version below 3.0, please import the following package: android.support.v4.*;
In the use of fragment activity please inherit fragmentactivity rather than activity. If your system is 3.0 or older, you will also need to import similar packages, but you can use normal activity.
2. Create a Fragment
Fragment support is used in different activity and can handle its own input events and lifecycle methods. Can be seen as a child activity.
Create a fragment
Creating a fragment is similar to creating an activity, inheriting the fragment class, overriding the lifecycle approach, and the main difference is the need to rewrite a Oncreateview () method to return the fragment layout. Example:
The fragment lifecycle approach relies on the life cycle of an activity, such as when the life cycle method of an activity OnPause () is invoked, all the Fragment OnPause () methods in this activity are also invoked.
For more information please refer to class fragment.
Adding fragment to an activity using XML
Although fragment can be reused by multiple activity, you must also associate fragment to a fragmentactivity. You can use the XML layout file to implement this association.
Description: The above mentioned fragmentactivity is applicable to the version of API below 3.0, and 3.0 and above versions can use ordinary activity.
Example:
Using the fragment tag above, android:name= "" Specifies a fragment that is added to the XML. For more information on creating different screen size layouts, please read support for different screen sizes.
When you add fragments to a layout XML file defined by an active layout, you cannot delete fragments at run time. If you intend to interact with the user and swap fragments, you must add active fragments to the activity when the first start.
3. Build a flexible UI
Fragmentmanager provides an action to add, delete, and replace fragment when the activity is running.
You can add fragment instead of defining them in an XML layout file while the activity is running. If you intend to change the life of fragment in an activity.
If you want to perform additions, deletions, and modifications, you must obtain a Fragmenttransaction object through the Fragmentmanager object, and use its APIs to perform these operations.
To add a fragment to an activity, you must add this fragment to a container view. Example:
In the activity you can get the fragment object by Getfragmentmanager () and then through the beginfragmenttransaction of the Fragmentmanager object () method to obtain the Fragmenttransaction object. Add a fragment to the current activity through its add () method.
A Fragmenttransaction object can perform multiple additions and deletions, and if you want to commit these changes to an activity, you must call the object's commit () method at the end. Example:
Because it is not defined in an XML layout, all operations that can transform deletes and modifications.
If you replace or delete a fragment and then allow the user to navigate to the previous fragment, you must call the Addtobackstack () method before calling the commit () method to add to the fallback stack. If you add this fragment to the fallback stack, the fragment will be stop instead of destroyed after the submission. If the user navigates to the fragment, the fragment is restart rather than recreated. If you do not add it to the fallback stack, it will be destroyed when it is deleted or replaced. Example:
4. Interaction with other fragment
There should be no communication between the two individual fragment. They should use the activity of their existence as a communication link.
To implement two fragment interactions, you can define an interface in fragment and then define a method in the interface that invokes the method in the Fragment Onattach () method. Then let the activity implement this method to complete the communication between the activity and the fragment. Example:
To define an interface and invoke the method:
Implements an interface in which you can interact with other fragment data:
You can find a fragment by Fragmentmanager's Findfragmentbyid ().
Fragmentmanager Findfragmentbyid return NUL solution
Look at two ways of generating fragment
I. Generating with XML tags
Adding XML tags to fragment host activity
<fragment
Android:id= "@+id/fragment_newscontent"
Android:name= "Com.firstcode.section4_news. Newscontentfragment "
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
The fragment class that name creates for you
This method fragment has generated at the time of activity creation
To obtain an action for a fragment instance in an activity:
Newscontentfragment fragment = (newscontentfragment) Getfragmentmanager (). Findfragmentbyid (R.id.fragment_ Newscontent);
ii. dynamic Generation with Java code
Add Framelayout to the view file of the host activity in fragment for placeholder
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/fragmentcontainer"/>
To generate fragment operations in an activity
Fragmentmanager fm = Getfragmentmanager ();
Fm.begintransaction ()
. Add (R.id.fragmentcontainer, "fragment class instance you created")
. commit ();
Problem Analysis:
I'm using the question of returning NUL with Fragmentmanager.findfragmentbyid right here, I'm generating fragment in a second way, which means that the arguments in Findfragmentbyid
I'm filling in the ID of the framelayout, not the fragment ID, so I'm going to return null.
Solution:
1. If it is statically generated fragment, obtain fragment instance with Getfragmentmanager (). Findfragmentbyid
2. If it is Java code dynamic generation fragment, get fragment instance directly new one is good no need to use Getfragmentmanager (). Findfragmentbyid
3. Note the tags in the XML file Framelayout and fragment
There's a problem, too, I solved it. Add text to TextView in fragment view
caused By:java.lang.NullPointerException:Attempt to invoke virtual method ' void Android.widget.TextView.setText ( java.lang.CharSequence) ' On a null object reference
The reason is that the ID parameter in fragment Add, generated by Java code, is filled with the ID of the fragment so fragment view is not generated
One of the best documents to understand the two fragment generation is Google's official Android training below is the Chinese translation
Add fragment Using XML tags
Add fragment dynamically using Java code