Android Fragment and Fragmentactivity usage

Source: Internet
Author: User


Android fragment is more than Android4.0, and fragmentactivity is for compatibility with the fragment of the 4.0 versions below.


So if you want to be compatible with Android version 4.0 below using fragment, the framework activity needs to inherit fragmentactivity,fragmentactivity this class is in ANDROID.SUPPORT.V4.APP.FRAGMENTACT In the ivity.


Here are 2 ways to use:


1. Inherit the activity.

(This only uses fragment for Android platforms over 4.0).


Framework activity:

Package Com.tandong.fragment;import Android.app.activity;import Android.app.fragment;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import android.widget.button;/** * Incompatible with 4.0 below mode fragment * * @author Tandong * */public class Mt_activity extends Activity implements Onclicklistener {private Button Btn_first, btn_s Econd;private Fragment Fragment_first, fragment_second;private fragmenttransaction fragmenttransaction;private Fragmentmanager Fragmentmanager; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (Savedinstancestate); Setcontentview (r.layout.fragment); Initview (); Fragment_second = new Fragment_second (); Fragmentmanager = This.getfragmentmanager (); fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_Second, " Second_fragment "). commit ();} Private VOID Initview () {Btn_first = (button) This.findviewbyid (r.id.btn_first); btn_second = (Button) This.findviewbyid (R.id.btn _second); Btn_first.setonclicklistener (this), Btn_second.setonclicklistener (this);} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.btn_first://loads a different fragmentif (null = = Fragment_ First) {Fragment_first = new Fragment_first ();} Fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_first, "fist_fragment"); Fragmenttransaction.commit (); Break;case r.id.btn_second:if (null = = Fragment_first {Fragment_second = new Fragment_second ();} Fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_second, "second_fragment"); Fragmenttransaction.commit (); break;default:break;}}}

Fragment Code:

Package Com.tandong.fragment;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class Fragment_Second Extends Fragment {      private View rootview; @Overridepublic view Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated method stubif (container==null) return null;rootview= Inflater.inflate (R.layout.fragment_two, Container,false); return rootview;}}


2. Inherit fragmentactivity (backwards compatible with version 4.0, use fragment, import the contents of the ANDROID.SUPPORT.V4 package)

Framework activity:

Package Com.tandong.fragment;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmenttransaction;import Android.view.view;import Android.view.View.OnClickListener; Import android.widget.button;/** * Compatible with 4.0 following modes fragment * * @author Tandong * */public class Mt_activity extends Fragmentac Tivity implements Onclicklistener {private Button Btn_first, Btn_second;private Fragment Fragment_first, Fragment_ Second;private fragmenttransaction fragmenttransaction;private Fragmentmanager FragmentManager; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.fragment); Initview (); Fragment_first = new Fragment_first (); Fragmentmanager = This.getsupportfragmentmanager (); fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace (r.id.main_fragment_layout, Fragment_first, "first_fragment"). commit ();} private void Initview () {Btn_first = (button) This.findviewbyid (r.id.btn_first); btn_second = (Button) This.findviewbyid (R.id.btn_second); Btn_first.setonclicklistener (this); Btn_second.setonclicklistener (this);} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.btn_first://loads a different fragmentif (null = = Fragment_ First) {Fragment_first = new Fragment_first ();} Fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_first, "fist_fragment"); Fragmenttransaction.commit (); Break;case r.id.btn_second:if (null = = Fragment_first {Fragment_second = new Fragment_second ();} Fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_second, "second_fragment"); Fragmenttransaction.commit (); break;default:break;}}}

Fragment Code:

Package Com.tandong.fragment;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class Fragment_first Extends Fragment {private View rootview; @Overridepublic view Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {if (container==null)   return null;rootview = Inflater.inflate (r.layout.fragment_first , Container,false); return rootview;}}


Original: http://www.aplesson.com/?p=377



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.