Primary Android Tutorial fragment to fragment communication

Source: Internet
Author: User

Here just give three classes rightfragment, Leftfragment, mainactivity in the simple code, as for layout how to set, do not repeat.

Train of thought: from the fragment one obtains and relies on the activity instance, the fragment one can invoke the function inside the activity, in the activity obtains the fragment second activity instance, the activity may use the fragment two function. Fragment one indirectly calls the functionality of fragment two.

First, Rightfragment:

Package Com.example.fragmenttest2;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class RightFragment Extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {                mainactivity activity = (mainactivity) getactivity ();//Get the activity instance that is backed by leftfragment Callleft = Activity.callleft (); Callleft.show ();//using the active function, this function is exactly the method of calling fragment two. View view = Inflater.inflate (R.layout.right_fragment, container, false); return view;} public void Show () {System.out.println ("rightfragment");}}

Second, mainactivity:

Package Com.example.fragmenttest2;import Android.os.bundle;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class MainActivity Extends Activity implements Onclicklistener {private leftfragment leftfragment; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button button = (button) Findviewbyid (R.id.button); Button button1 = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (this); Button1.setonclicklistener ( this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.button://button one, which implements a replacement fragment function anotherrightfragment fragment = new Anotherrightfragment (); Fragmentmanager Fragmentmanager = Getfragmentmanager (); Fragmenttransaction transaction = Fragmentmanager.begintransaction (); Transaction.replace (R.id.right_laYout, fragment); Transaction.addtobackstack (null); Transaction.commit (); Break;case r.id.button1:leftfragment = ( leftfragment) Getfragmentmanager ()//button Two, you can directly use the left fragment is the function of fragmentation two. Findfragmentbyid (r.id.left_fragment); Leftfragment.show (); break;default:break;}} public void Show () {System.out.println ("mainactivity");} Public Leftfragment Callleft () {///extract method, call fragment two function leftfragment = (leftfragment) Getfragmentmanager (). Findfragmentbyid ( r.id.left_fragment); return leftfragment;}}

Third, Leftfragment:

Package Com.example.fragmenttest2;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class LeftFragment Extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {View view = Inflater.inflate (R.layout.left_fragment, container, false); return view;} public void Show () {//Fragment II function, here only to demonstrate knowledge print a line of output System.out.println ("Leftfragment");}}

Start the program and discover a straight line of printed output:

Leftfragment

This enables fragmentation and activity, as well as direct communication between fragments and fragments.



Primary Android Tutorial fragment to fragment communication

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.