Android Fragment Static Implementation example

Source: Internet
Author: User

Fragment is part of the UI for activity, which is embedded in activity, where multiple fragment can divide an activity into multiple parts, which are used more in large screen phones or tablets. This will not use multiple activity to switch the trouble. Of course, fragment can also not display, only in the background processing some data, this article does not talk about this for the time being. Here's how to statically add fragment to the activity's layout file.

Custom fragment usually inherit the fragment class, and there are some special inheritance listfragment,dialogfragment. Inheriting the fragment class typically implements three methods: OnCreate (), Oncreateview (), OnPause ();

I defined two fragment in activity, one on the left leftfragment and one on the right rightfragment. Here's the code: first we're going to implement our own fragment class

Leftfragment class :

 Public classLeftfragmentextendsfragment{@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); System.out.println ("Leftfragment onCreate"); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {System.out.pri Ntln ("Leftfragment Oncreateview"); //The first parameter is the interface layout that the fragment will display, the second parameter is the activity that this fragment belongs to, and the third argument is whether the fragment is attached to the activity        returnInflater.inflate (R.layout.leftfragment, container,true); } @Override Public voidOnresume () {Super. Onresume (); System.out.println ("Leftfragment Onresume"); } @Override Public voidOnPause () {Super. OnPause (); System.out.println ("Leftfragment OnPause"); } @Override Public voidOnStop () {Super. OnStop (); System.out.println ("Leftfragment OnStop"); }}

Several callback functions are implemented here, mainly to see the relationship between activity and fragment life cycle. Where the Oncreateview () method is to return the layout of the fragment corresponding to the layout of the activity for the activity to load. inflater.inflate (R.layout.leftfragment, container, True) The first parameter in the R.layout.leftfragment method is the fragment corresponding layout file ID, the second parameter container is the target activity to be inserted, The third parameter is to determine whether the fragment is attached to this container.

Leftfragment the corresponding layout file:

<?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= "@android: Color/holo_orange_dark"android:orientation= "Vertical" > <Button Android:id= "@+id/previous_button"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/previous_button"/> <Button Android:id= "@+id/next_button"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/next_button"/> <Button Android:id= "@+id/exit_button"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/exit_button"/> </LinearLayout>

Rightfragment class: Similar to Leftfragment

 Public classRightfragmentextendsFragment {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); System.out.println ("Rightfragment onCreate"); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {SYSTEM.OUT.P Rintln ("Rightfragment Oncreateview"); returnInflater.inflate (R.layout.rightfragment, container,true); } @Override Public voidOnresume () {Super. Onresume (); System.out.println ("Rightfragment Onresume"); } @Override Public voidOnPause () {Super. OnPause (); System.out.println ("Rightfragment OnPause"); } @Override Public voidOnStop () {Super. OnStop (); System.out.println ("Rightfragment OnStop"); } }

Rightfragment the corresponding layout file:

<?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:orientation=" vertical ">        <TextView  android:id=" @+id/show_message "           android:layout_width= "Fill_parent"         android:layout_height= "Fill_parent"          android:background= "@android: Color/holo_blue_dark"         android:text= "@string/show_ Message "/>  </LinearLayout>

Finally, the activity's layout file:

 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "3 android:layout_width=" fill_parent "4 android:layout_height=" fill_parent "5 android:orientation= "Horizontal" > 6 7 <fragment  8 android:id= "@+id/left_fragment" 9 A Ndroid:name= "Com.sunflower.LeftFragment" android:layout_width= "match_parent" android:layout_height= "fi Ll_parent "android:layout_weight=" 3 "/>13 <fragment  android: Id= "@+id/right_fragment" android:name= "Com.sunflower.RightFragment" android:layout_width= "Android:layout_height=" Fill_parent "android:layout_weight=" 1 "/>20 </linearlayout> 

Add the fragment tag to the layout file in the activity, where the Android:name property corresponds to the full name of the custom fragment class, and the system will specify the Oncreateview () of the fragment according to this call method to get the layout of this fragment and then join the activity. The container parameter in the Oncreateview () method is passed in the past.

Look at the results shown:



When you open a program, the life cycle displays:


The life cycle is displayed when you press the back key:

Android Fragment Static Implementation example

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.