Previous Article This paper introduces the life cycle of fragment, and generally understands that the life cycle of fragment is closely related to the activity that binds to it, and this article studies the communication between fragment .
Talk not much, learn by example:
Define two fragment and let them show up in the same activity, noting that only two fragment are in the same activity when they communicate with each other
Fragment1.xml
<?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:o rientation= "vertical" android:background= "#ffff00" > <button android:id= "@+id/btfragment1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= " Get fragment EditText value " /> <textview android:id=" @+id/tvfragment1 "android:layout_width = "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
Fragment2.xml
<?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:o rientation= "vertical" android:background= "#00ff00" > <edittext android:id= "@+id/etfragment2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
Fragment1.java
public class Fragment1 extends fragment{@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return inflater.inflate (r.layout.fragment1,container,false);} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); Button button = (button) getactivity (). Findviewbyid (R.id.btfragment1); Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {EditText et = (EditText) getactivity (). Findviewbyid ( R.ID.ETFRAGMENT2); TextView TV = (TextView) getactivity (). Findviewbyid (R.id.tvfragment1); Tv.settext (Et.gettext ());// From the edittext inside the Fragment2 to get the value displayed in the TextView of Fragment1}});}}
Fragment2.java
public class Fragment2 extends fragment{@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return inflater.inflate (R.layout.fragment2,container,false);}}
layout file to display fragment: Fragment_communicate.xml
<?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:o rientation= "vertical" > <fragment android:name= "Com.hzw.programmingtest.Fragment1" android: Layout_width= "Match_parent" android:layout_height= "0px" android:layout_weight= "1"/> < Fragment android:name= "Com.hzw.programmingtest.Fragment2" android:layout_width= "Match_parent " android:layout_height= "0px" android:layout_weight= "1"/></linearlayout>
activity to display the layout file:
public class Communicateactivity extends fragmentactivity{@Overrideprotected void OnCreate (Bundle arg0) { Super.oncreate (arg0); Setcontentview (r.layout.fragment_communicate);}}
Program Run Result:
Communication between Android-----fragment