Communication between Android-----fragment

Source: Internet
Author: User

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

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.