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