Android fragment dynamic Create detailed and sample code _android

Source: Internet
Author: User

Android Fragment Dynamic Creation

Fragment is part of or an action in the interface of an activity. Multiple fragment can be combined into one activity to create a multiple interface and to reuse a fragment in multiple activity. An activity that can be modularized into a fragment task that has its own lifecycle, receives its own events, and can be added or deleted when the activity is run.

Fragment cannot exist independently, it must be embedded in activity, and the life cycle of fragment is directly affected by the activity in which it resides. For example: When an activity is suspended, all the fragment he owns are suspended, and when the activity is destroyed, all the fragment he owns are destroyed. However, when the activity runs (after Onresume (), OnPause (), each fragment can be manipulated individually, such as adding or removing them. When performing the above transaction for fragment, the transaction can be added to a stack, which is managed by the activity, and each of the stacks is a fragment transaction. With this stack, you can reverse fragment transactions so that you can support the "Back" key (navigate backwards) at the fragment level.

When you add a fragment to an activity, it must be placed in the ViewGroup control, and you need to define the fragment's own interface. You can declare fragment in the Layout.xml layout file,:<fragment>; can also create fragment in your code, and then add it to the ViewGroup control. However, fragment does not necessarily have to be placed in the activity interface, it can be hidden in the background for activities.

Real combat.

Project layout file Code:

<linearlayout xmlns: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:orientation= "Horizontal"
  tools:context= ". Mainactivity ">

  <fragment
    android:id=" @+id/fragment1 "
    android:name=" Com.wuyudong.fragment.Fragment1 "
    android:layout_width=" 0dip "
    android:layout_height=" Fill_parent "
    android:layout_weight= "1" >
  </fragment>

  <fragment
    android:id= "@+id/fragment2"
    android:name= "Com.wuyudong.fragment.Fragment2"
    android:layout_width= "0dip"
    android:layout_ height= "Fill_parent"
    android:layout_weight= "1" >
  </fragment>

</LinearLayout>

Then create a new two Fragment.xml file under the Layout folder

Fragment1.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: Background= "#0000ff"
  android:orientation= "vertical" >
</LinearLayout>

Fragment2.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: Background= "#ff00"
  android:orientation= "vertical" >
</LinearLayout>

Next, create a new two Java files under the project Source code folder

Fragment1.java

public class Fragment1 extends Fragment {
  /**
   * When Fragment is created, the method invoked returns the contents of the current Fragment display 
   * *
  @Override Public
  View Oncreateview (layoutinflater inflater, ViewGroup container,
      Bundle savedinstancestate) {
    Return inflater.inflate (r.layout.fragment1, null);
  }



Fragment2.java

public class Fragment2 extends Fragment {
  /**
   * When Fragment is created, the method invoked returns the contents of the current Fragment display 
   * *
  Override public
  View Oncreateview (layoutinflater inflater, ViewGroup container,
      Bundle savedinstancestate) {return
    inflater.inflate (r.layout.fragment2, null);
  }

}

Running the project

Next, implement dynamic creation fragment

Build a new project based on the project that you just made. Delete the fragment1 and Fragment2 code snippet from the original Activity_main.xml code, and the other code is unchanged

Next, add the following code to the mainactivity:

public class Mainactivity extends activity {

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);

    1, the current mobile phone to determine the direction of
    int width = Getwindowmanager (). Getdefaultdisplay (). getwidth ();
    int height = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();
    Fragment1 fragment1 = new Fragment1 ();
    Fragment2 Fragment2 = new Fragment2 ();

    Fragmentmanager fm = Getfragmentmanager ();
    Fragmenttransaction ft = fm.begintransaction ();

    if (width > height) {
      //Horizontal Direction
      Ft.replace (Android). R.id.content, fragment1);
    else {
      Ft.replace (Android. R.id.content, Fragment2);

    Ft.commit ();

  }



The code above implements the different fragment that are displayed when the phone is facing differently.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.