Android (fragment) use the following

Source: Internet
Author: User

One. Fragment use:

To manage Fragment in your activity , you need to use Fragmentmanager, which can be Getfragmentmanager (), Note here if you want to use the Getsupportfragmentmanager () method in the V4 package.

Fragmentmanager can open a thing call BeginTransaction () method to return Fragmenttransaction object, this is we can use fragmenttransaction to run delete add Some of the actions of fragment:

(1). Add (): Go to activity and add a Fragment .


(2). Remove (): Removes one from activity Fragment, when this Fragment is not added to the fallback stack, it will be destroyed.

The view layer is destroyed when added to the fallback stack, and Ondestoryview and Oncreateview are called when the fragment is displayed.


(3). Replace (): Use a fragment to replace the current Fragment, which is the remove operation and the add-fit operation.


(4). Hide (): Hides the current fragment, but is set to not visible and will not be destroyed.


(5). Show (): Displays the previously hidden fragment.


(6). Detach (): The view will be removed from the UI, unlike remove (), at which point the state of the fragment is maintained by Fragmentmanager.


(7). Attach (): Rebuild view. Attached to the UI and displayed.


(8). Addtobackstack (): Joins the fragment transaction to the fallback stack.

Null indicates the current Fragment.


(9). Commit (): Commit the transaction.


Note that after adding Fragment with add , you just want to hide Fragment and keep the current Fragment input, you only need to use hide (), assuming you don't want the user to see the data directly using Replace () is more convenient than using Add () in Remove ().

Two. How to use the fallback stack to persist data:

To achieve the effect, when the first Fragment after the Click Change Text button, will be displayed in the center of the screen TextView content changes, and then click into the second Fragment, click Back button, The TextView content in the center of the screen will revert to the unchanged one, which means that the return stack destroys the fragment view, but the instance is not destroyed. Suppose you want to not destroy the data, just like the one above says, use Hide () and show ().

The picture will not be uploaded. Everyone but self-test source code (to source code to leave the mailbox, this is based on the previous article modified):

Main activity:

public class Mainactivity extends Activity  {relativelayout r1; Relativelayout R2; Relativelayout R3; Relativelayout view = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.bottom_layout); r1 = (relativelayout) Findviewbyid (R.ID.LAYOUT1); r2 = ( Relativelayout) Findviewbyid (R.ID.LAYOUT2); r3 = (relativelayout) Findviewbyid (R.ID.LAYOUT3); SetDefaultFragment ();} private void Setdefaultfragment () {Fragmentmanager fm = Getfragmentmanager (); Fragmenttransaction transaction = Fm.begintransaction (); Myfragment my = new Myfragment (); Transaction.add (R.ID.FRAME_LAYOUT1, My, "one"); Transaction.addtobackstack (null); Transaction.commit ();}}

Fragment1:

public class Myfragment extends Fragment implements Onclicklistener {Private button Button1;private button Button2;privat e TextView textView1; @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {System.out.println ("Oncreateview"); View view = Inflater.inflate (R.layout.fragment_1, container, false); button1 = (Button) View.findviewbyid (R.id.button1) ; Button1.setonclicklistener (this); button2 = (Button) View.findviewbyid (R.id.button2); Button2.setonclicklistener ( this); textView1 = (TextView) View.findviewbyid (r.id.textview1); return view;} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.button1:textview1.append ("Ha"); break;case R.id.button2:myfragment2 F2 = new MyFragment2 (); Fragmentmanager fm = Getfragmentmanager (); Fragmenttransaction tx = Fm.begintransaction (); Tx.replace (R.ID.FRAME_LAYOUT1, F2, "a"); Tx.addtobackstack (null); Tx.commit (); break;}}}

Fragment2:

public class MyFragment2 extends Fragment implements Onclicklistener {Private button Button1;private button Button2;priva Te TextView textView1; @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {System.out.println ("Oncreateview"); View view = Inflater.inflate (R.layout.fragment_2, container, false) button1 = (Button) View.findviewbyid (r.id.button11 ); Button1.setonclicklistener (this); button2 = (Button) View.findviewbyid (R.ID.BUTTON21); Button2.setonclicklistener (this); textView1 = (TextView) View.findviewbyid (R.ID.TEXTVIEW2); return view;} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.button11:textview1.append ("Ha"); break;case R.id.button21:myfragment3 F3 = new MyFragment3 (); Fragmentmanager fm = Getfragmentmanager (); Fragmenttransaction tx = Fm.begintransaction (); Tx.hide (this); Tx.add (R.ID.FRAME_LAYOUT1, F3, "three");//Tx.replace ( R.id.id_content, Fthree, "three"); Tx.addtobackstack (null); Tx.commit (); break;}}}
Fragment1 layout:

<?xml version= "1.0" encoding= "Utf-8"?

><relativelayout 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/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:layout_centerinparent=" true " android:text=" first page "/> <button android:id = "@+id/button1" android:layout_width= "fill_parent" android:layout_height= "40DP" android:text= " Change the text "/> <button android:id=" @+id/button2 " android:layout_width=" Fill_parent " Android : layout_height= "40DP" android:layout_below= "@id/button1" android:text= "Jump Second Interface"/> </ Relativelayout>


Fragment3:

public class MyFragment3 extends fragment{    @Override public    View Oncreateview (Layoutinflater inflater, ViewGroup container,    Bundle savedinstancestate) {    return inflater.inflate (R.layout.fragment_3, container, false);}    }

This will be OK.


Three. callback between activity and fragment:

Due to the repeated use of fragment, it is necessary to reduce the coupling between fragment and activity, and fragment should not directly manipulate other fragment, after all, fragment operation should be determined by its manager activity.


just use the example above, The first kind of callback:

public class Myfragment extends Fragment implements Onclicklistener {Private button Button1;private button Button2;privat e TextView textView1; @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {System.out.println ("Oncreateview"); View view = Inflater.inflate (R.layout.fragment_1, container, false); button1 = (Button) View.findviewbyid (R.id.button1) ; Button1.setonclicklistener (this); button2 = (Button) View.findviewbyid (R.id.button2); Button2.setonclicklistener ( this); textView1 = (TextView) View.findviewbyid (r.id.textview1); return view;}  public interface myfragmentoneclick{void Onmyonebtnclick (); } @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.button1:textview1.append ("Ha"); break;case R.ID.BUTTON2://First callback method if (getactivity () instanceof Myfragmentoneclick) {(Myfragmentoneclick)          Tivity ()). Onmyonebtnclick (); } Break;}}}


Another kind of callback:

public class MyFragment2 extends Fragment implements Onclicklistener {Private button Button1;private button Button2;priva Te TextView textview1;private myfragmenttwoclick myfragmenttwoclick; @Overridepublic View Oncreateview (layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {System.out.println ("Oncreateview"); View view = Inflater.inflate (R.layout.fragment_2, container, false) button1 = (Button) View.findviewbyid (r.id.button11 ); Button1.setonclicklistener (this); button2 = (Button) View.findviewbyid (R.ID.BUTTON21); Button2.setonclicklistener (this); textView1 = (TextView) View.findviewbyid (R.ID.TEXTVIEW2); return view;}  public interface myfragmenttwoclick{void Onmytwobtnclick (); }//Another kind of callback method. Set callback interface public void Setmytwobtnclicklistener (Myfragmenttwoclick myfragmenttwoclick) {This.myfragmenttwo      Click = Myfragmenttwoclick; } @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case r.id.button11:textview1.append ("Ha"); Break;case R . id.button21: if (Myfragmenttwoclick! = null) {Myfragmenttwoclick.onmytwobtnclick (); } Break;}}}

Main activity implementation:


public class Mainactivity extends Activity implements Myfragmentoneclick,myfragmenttwoclick {relativelayout r1; Relativelayout R2; Relativelayout R3; Relativelayout view = null; Myfragment F1; MyFragment2 F2; MyFragment3 f3; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.bottom_layout); r1 = (relativelayout) Findviewbyid (R.ID.LAYOUT1); r2 = (relativelayout) Findviewbyid ( R.ID.LAYOUT2); r3 = (relativelayout) Findviewbyid (R.ID.LAYOUT3); Setdefaultfragment ();} private void Setdefaultfragment () {Fragmentmanager fm = Getfragmentmanager (); Fragmenttransaction transaction = Fm.begintransaction (); f1 = new Myfragment (); Transaction.add (R.ID.FRAME_LAYOUT1, F1, "One"); Transaction.addtobackstack (null); Transaction.commit ();}  @Overridepublic void Onmyonebtnclick () {if (F2 = = null) {F2 = new MyFragment2 ();          F2.setmytwobtnclicklistener (this); } Fragmentmanager FM = Getfragmentmanager (); Fragmenttransaction tx = Fm.begintranSaction (); Tx.replace (R.ID.FRAME_LAYOUT1, F2, "a"); Tx.addtobackstack (null); Tx.commit ();}            @Overridepublic void Onmytwobtnclick () {if (F3 = = null) {F3 = new MyFragment3 (); } Fragmentmanager FM = Getfragmentmanager (); Fragmenttransaction tx = Fm.begintransaction (); tx.hide (F2); Tx.add (R.ID.FRAME_LAYOUT1, F3, "three"); Tx.addtobackstack (null); Tx.commit ();}}
This also achieves the above functions.





Android (fragment) use the following

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.