1. Basic Concepts
I. What is a transaction:
A transaction is a rigorous set of operations in an application, and all operations must be completed successfully, or all changes made in each operation will be undone. A sequence of operations in a transaction either succeeds or does not. The end of a transaction has two types, and when all the steps in the transaction are executed successfully, the transaction commits. If one of the steps fails, a rollback operation occurs, and the undo action is undone until the start of the transaction.
Two. What is fragment:
Fragment is a new API introduced after Android3.0, he appeared in order to adapt to the big screen tablet, of course, now he is still the Tablet app UI design darling, and our common mobile phone development will join this fragment, We can think of him as a small activity, also known as activity footage! Think, if a very large interface, we have a layout, write the interface will be more trouble, and if the component is more than the management is also very troublesome! and using fragment we can divide the screen into pieces, then group it, and do a modular management! This makes it more convenient to dynamically update the user interface of the activity during operation. In addition, fragment cannot be used alone, he needs to be nested in the activity , although he has his own life cycle, but still be affected by the life cycle of the host activity, such as the activity was destory destroyed , he will also follow the destruction.
2. Life cycle
3. Code
Note: Because fragment is part of the activity, you do not need to register in Manifest.xml.
Activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Sample.android_serialport_api.adapter.MainActivity"> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Key 1"Android:id= "@+id/fbt1"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Key 2"Android:id= "@+id/fbt2"/> <FramelayoutAndroid:id= "@+id/mycontainer" //activity in which fragment is required to be placed, add one, like a container Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > </Framelayout></LinearLayout>
Fragment_blank.xml
<Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/colorprimary"Tools:context= "Sample.android_serialport_api.adapter.BlankFragment1"> <!--todo:update blank Fragment layout - <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:text= "I am framgment" /></Framelayout>
Mainactivity.java
Public classMainactivityextendsappcompatactivity {Button bt1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); BT1=(Button) Findviewbyid (R.ID.FBT1);Bt1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {System.out.println ("Key 1"); GotoFragment1 (); } }); } Private voidgotoFragment1 () {System.out.println ("Fragment 1"); //get fragment's managerFragmentmanager FM =Getfragmentmanager (); //Start a transactionFragmenttransaction ft =fm.begintransaction (); //Create our fragment instancesBlankFragment1 BF =NewBlankFragment1 (); Ft.replace (R.id.mycontainer, BF); Ft.commit (); }}
Blankfragment1.java
Public class extends Fragment { public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { // return a view because fragment does not have a view return false ); }}
Android Components-fragments (fragment)