Binding of Fragment and Activity in android Learning

Source: Internet
Author: User

Binding of Fragment and Activity in android Learning
Recently, the project is busy, and the boss won't let me do android anymore. In short, there will be a lot of things for quick development. As long as the problem can be solved, it is a good way, but from a personal perspective, quick development makes it impossible to gain an in-depth understanding of some technologies. Although android may not be used in projects now, do not give up and continue learning as you start.

Fragment is called Fragment in android. The introduction of Fragment is mainly to solve multiple screen adaptation problems of android open source. When android is used for tablets, mobile phones, and TVs, using activity alone leads to a single UI. Fragment is introduced in android sdk11 to solve this problem. Fragment must be bound to the activity to run.

Fragment and Activity have a certain degree of similarity and a similar life cycle. However, Fragment must be attached to the Activity. At the same time, the Activity cannot be replaced or deleted, and Fragment can be replaced or deleted.

Fragment can be bound to the corresponding fragment through the UI control Fragment or dynamically added. The first method is relatively simple, while the second method is more flexible. It is usually a reasonable choice in actual use. Fragment is more flexible than Activity. Therefore, AUF is currently favored in android design, that is, Fragment is used by all users.

The first method is usually used to display the corresponding Fragmet information in the XML layout file.

 
 
  1. <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/helloFragment"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:name="com.example.hell.HelloFragment">
  6. </fragment>
Android is the corresponding Fragment class name, which cannot be easily extended or canceled.

In recent studies, we have better experienced the MCV idea of android design. M is usually a model related to exponential data, that is, the design of relevant data modules, this design will make the data independent from other components. C refers to the control module. This module will realize the association of UI display and data to achieve specific control association, V indicates the display function that displays relevant data information through control. Therefore, in the process of android function design, it is best to take the relevant data information as a separate class and provide a single instance tool class for managing the data. Through this tool class, control related components to access the data.

These are just my thoughts after reading some code. The idea of AUF usually makes it necessary to create a corresponding Fragment object during the process of creating an activity. The layout of the corresponding activity only provides a simple Fragment container.
 
 
  1. <?xml version="1.0" encoding='utf-8'?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/fragmentContainer"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. />
The fragment layout is the ui layout that needs to be displayed.
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >

  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/crime_title_label"
  10. style="?android:listSeparatorTextViewStyle"
  11. />

  12. <EditText
  13. android:id="@+id/crime_title"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:hint="@string/crime_title_hint"
  17. />

  18. <TextView
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="@string/crime_details_label"
  22. style="?android:listSeparatorTextViewStyle"
  23. />

  24. <Button
  25. android:id="@+id/crime_date"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:layout_marginLeft="16dp"
  29. android:layout_marginRight="16dp"
  30. />

  31. <CheckBox
  32. android:id="@+id/crime_solved"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_marginLeft="16dp"
  36. android:layout_marginRight="16dp"
  37. android:text="@string/crime_solved_label"
  38. />

  39. </LinearLayout>
The advantage of this design is that the activity does not need to be specific to the ui layout, more like a general layout, and all fragment can be added. Fragment requires a specific UI layout. In this way, the activity and the specific UI display are independent from each other, and Fragment dynamic addition and UI switching are realized. Therefore, activity manages Fragment more.

Implementation of Fragment binding
In the implementation of AUF, a common method is the single Fragment Activity. That is, only one Fragment is bound to the Activity. Therefore, a common method can be provided to bind the fragment to the activity, that is, to bind a single Fragment Activity class. In this way, all Fragment needs to be bound to a specific Activity, and only the corresponding Fragment object needs to be created. This abstract class is complete for binding operations. The implementation of adding Fragment to the code will be used by a Fragment Manager. The operations of this manager are similar to the transaction processing process of the database. The specific implementation process is as follows:
 
 
  1. Public abstract class SingleFragmentActivity extends FragmentActivity {
  2. Protected abstract Fragment createFragment (); <---- this method is used to create different Fragment objects.

  3. @ Override
  4. Protected void onCreate (Bundle arg0 ){
  5. Super. onCreate (arg0 );
  6. SetContentView (R. layout. activity_fragment );
  7. /* Obtain the manager */
  8. FragmentManager fm = getSupportFragmentManager ();
  9. Fragment fragment = fm. findFragmentById (R. id. fragmentContainer); // R. id. fragmentContainer is the container

  10. If (fragment = null ){
  11. /* Add the corresponding Fragment process to the activity */
  12. Fragment = createFragment ();
  13. Fm. beginTransaction () // the specific operation is similar to the transaction processing process of the database.
  14. . Add (R. id. fragmentContainer, fragment)
  15. . Commit ();
  16. }
  17. }
  18. }
In the specific process of implementing an activity, you only need to implement the corresponding abstract function Fragment createFragment (). At the same time, all the activities can be used in the same layout file. The code above implements a binding process between Fragment and activity, that is, FragmentManager is required for control.
 
 
  1. Public class CrimeActivity extends SingleFragmentActivity {

  2. @ Override
  3. Protected Fragment createFragment (){
  4. // Implement the specific creation process
  5. }
  6. }
This is very common for the activity, so in this case of a single fragment, you can use a general activity.

The createFragment implementation method can actually implement code for different activities, but in actual use, the Fragment of one activity is used to start another activity, therefore, some parameters may be passed, and these parameters are for activity, and in order to implement the AUF idea, the corresponding Fragment should be started during the activity process.

Sometimes Fragment has some parameter information that belongs to the current object to be saved. Of course, it can be saved to the belonging activity. However, considering uniformity, it is usually considered to add the Bundle parameter of Fragment, the Bundle parameter of Fragment is similar to the Bundle parameter of Activity, which is used to save important information. However, the Bundle parameter of Fragment needs to be bound before being added to acitment. Considering the versatility of Fragment, you can add a corresponding Bundle argement for each created Fragment. The details are as follows:
 
 
  1. PublicclassCrimeFragment extends Fragment {
  2. ...
  3. /* This is the basic rule for implementing bundle in fragment. Do not call constructor during fragment creation */
  4. /* This function must be a static method. Otherwise, you must first create the corresponding object to create fragmet */
  5. Public static CrimeFragment newInstance (UUID crimeId ){
  6. Bundle args = new Bundle (); // create a bundle parameter object
  7. Args. putSerializable (EXTRA_CRIME_ID, crimeId); // fill in the corresponding key-word pair

  8. CrimeFragment fragment = new CrimeFragment (); // create the corresponding Fragment class
  9. Fragment. setArguments (args); // bundle parameter of the State pair

  10. Return fragment;
  11. }
  12. ...
  13. }
In this way, you can implement the corresponding createfragment method in the corresponding activity. The specific implementation is as follows:
 
 
  1. Public class CrimeActivity extends SingleFragmentActivity {

  2. @ Override
  3. Protected Fragment createFragment (){
  4. UUID crimeId = (UUID) getIntent ()
  5. . GetSerializableExtra (CrimeFragment. EXTRA_CRIME_ID );
  6. // This parameter is from activity and passed to Fragment through newInstance
  7. Return CrimeFragment. newInstance (crimeId );
  8. }
  9. }
Although the newInstance () implementation method can also be completed in the createFragment function of the Activity, considering reusability, the Fragment binding relationship is more universal by Fragment.

In this way, you can start the Fragment of another Activity in the Fragment of one Activity. The basic implementation process is as follows.

SinalFragmentActivity (BIND Fragment to Activity)
|
V
XXXActivity (to create a specific Fragment)
|
V
Obtain the activity parameters, and then call the creation function of the Bundle bound to Fragment.


XXXFragment
|
V
Defines the static instance creation method, which binds bundle and Fragment.

Therefore, the implementation of the Activity class is as follows:
 
 
  1. Public class CrimeActivity extends SingleFragmentActivity {

  2. @ Override
  3. Protected Fragment createFragment (){
  4. UUID crimeId = (UUID) getIntent ()
  5. . GetSerializableExtra (CrimeFragment. EXTRA_CRIME_ID );
  6. // Obtain the activity parameters and bind the corresponding parameters with and Fragment through the newInstance Function
  7. Return CrimeFragment. newInstance (crimeId );
  8. }
  9. }
This implementation method better implements the binding of single Activity and Fragment in AUF. This binding implementation is very common for the layout of the Activity, and the defined abstract class also implements the binding relationship between Fragment and Activity. You only need to create the corresponding Fragment. For further implementation, Fragment is required to provide a static method for binding Bundle to create an object and bind a bundle object.

This implementation method is mainly to follow the implementation of AUF. In fact, it does not need to be so complicated, but from the perspective of reusability, this implementation method is very good.

You can read some excellent analyses.
Http://www.cnblogs.com/xinye/archive/2012/08/28/2659712.html
Http://blog.csdn.net/guolin_blog/article/details/8881711

References:
Android Programming: The Big Nerd Ranch Guide Chapter 10th


Related Article

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.