Communication between the android_fragment and the activity

Source: Internet
Author: User

The life cycle of Fragment is changed with the change of activity.

If the activity is to give fragment to the data when it is running, it needs to fragment implement a custom interface, and implement the method inside the interface, save the interface in the activity and call this method when the data is needed.

What if you need to pass data to the activity when the fragment is running? The activity implements this interface first, then overrides the Onattach (Activity Act) method inside the fragment, and in this method the activity is cast to the interface class, and then the method of invoking the interface class when the fragment is used.

Take a look at the following example:

Activity to fragment inside the descendant data:

 Public classMainactivityextendsactivity{PrivateTestfragment fragment;//Fragment    PrivateUpdatefragment updatefragment;//interfaces implemented by the fragment@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.main_layout);            Initview (); }        Private voidInitview () {fragmenttransaction fragmenttransaction=Getfragmentmanager (). BeginTransaction (); Fragment=Newtestfragment (); Updatefragment=fragment;        Fragmenttransaction.add (R.id.main, fragment); Fragmenttransaction.addtobackstack (NULL);    Fragmenttransaction.commitallowingstateloss (); } @Overrideprotected voidOnresume () {//change the value of TextView inside the fragmentUpdatefragment.setdata ("Harry"); Super. Onresume (); }}

Interface class

 Public Interface updatefragment {    void  setData (String str);}

Fragment class:

 Public classTestfragmentextendsFragmentImplementsupdatefragment{Privateview view; PrivateTextView txt;  Publictestfragment () {Super(); } @Override Public voidOnattach (activity activity) {Super. Onattach (activity); } @Override Public voidonviewcreated (view view, Bundle savedinstancestate) {Super. onviewcreated (view, savedinstancestate); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {if(NULL==view) {View= Inflater.inflate (R.layout.activity_main, container,false); TXT=(TextView) View.findviewbyid (r.id.tv); }        returnview; } @Override Public voidsetData (String str) {if(NULL!=txt)        {txt.settext (str); }    }}

Fragment for activity incoming data demo:

Activity class:

 Public classMainactivityextendsActivityImplementsupdatefragment{Private Static FinalString TAG = mainactivity.class. Getsimplename (); PrivateTestfragment fragment;//Fragment@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.main_layout);            Initview (); }        Private voidInitview () {fragmenttransaction fragmenttransaction=Getfragmentmanager (). BeginTransaction (); Fragment=Newtestfragment ();        Fragmenttransaction.add (R.id.main, fragment); Fragmenttransaction.addtobackstack (NULL);    Fragmenttransaction.commitallowingstateloss (); } @Overrideprotected voidOnresume () {Super. Onresume (); } @Override Public voidsetData (String str) {if(NULL!=str) {LOG.D (TAG,"SetData:" +str); }    }

The interface class is the same as above and it's not written.

Fragment class:

 Public classTestfragmentextendsfragment{Privateview view; PrivateTextView txt; PrivateButton btn; Privateupdatefragment updatefragment;  Publictestfragment () {Super(); } @Override Public voidOnattach (activity activity) {Try{updatefragment=(updatefragment) activity; } Catch(Exception e) {e.printstacktrace (); }        Super. Onattach (activity); } @Override Public voidonviewcreated (view view, Bundle savedinstancestate) {Super. onviewcreated (view, savedinstancestate); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {if(NULL==view) {View= Inflater.inflate (R.layout.activity_main, container,false); TXT=(TextView) View.findviewbyid (r.id.tv); BTN=(Button) View.findviewbyid (R.ID.BTN);        Btn.setonclicklistener (Clicklistener); }        returnview; }    PrivateOnclicklistener Clicklistener =NewOnclicklistener () {@Override Public voidOnClick (View v) {if(NULL!=txt)            {Updatefragment.setdata (Txt.gettext (). toString ()); }                    }    };}

Communication between the android_fragment and the activity

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.