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