The next step is to transfer data to the fragment to the activity. The main idea is to create a callback interface in fragment, which is used to implement the function of fragment transferring data to activity.
callback function (interface)
Before learning to use the callback interface to implement fragment to the activity to transfer data, the first thing to understand the callback function, the following reference to the user Futeng answer, invasion delete: https://www.zhihu.com/question/19801131/ answer/26586203.
Simply put, the callback function is when you call method B of Class B in a Class A, and Method B in turn callback method A in a, and this method a becomes the callback method. One of the characteristics of the callback method is that when the passed-in parameter is an interface, different functions can be produced depending on the interface implementations of the different classes passed in.
Specific methods
Once we understand the callback function, we can use the callback interface to implement fragment data transfer to the activity. The brief idea is to define a callback interface in fragment and instantiate the interface as an Activity object when Onattach (). Then, implementing the interface in the activity allows fragment to get the contents of the interface when Onattach. Finally, in the appropriate location of the fragment, callback the interface, you can realize the fragment to the activity of data transfer function.
Code implementation One, defining interfaces in fragment
The callback interface is defined in fragment and instantiated in Onattach (note that the activity activity method, which is discarded after Android3.0, requires overloading Onattach (context context), and import v4. Fragment bag)
Public Interface mlistener{ publicvoid fragmentsend ( String text); Private Mlistener Mlistener; @Override publicvoid Onattach (context context) { super . Onattach (context); Mlistener= (mlistener) context; }
Second, implement the interface in activity
Public classActivity4extendsAppcompatactivityImplementsFragment4.mlistener {PrivateTextView TV; @Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_4); TV=(TextView) Findviewbyid (R.ID.A4TV); Fragment4 Fragment4=NewFragment4 (); Fragmentmanager Supportfragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction fragmenttransaction=supportfragmentmanager.begintransaction (); Fragmenttransaction.add (R.ID.A4LAYOUT,FRAGMENT4); Fragmenttransaction.commit (); } @Override Public voidfragmentsend (String text) {tv.settext (text); }}
Third, at a specific location in the fragment, callback the interface
In this case, the function is to send the contents of the text box to the activity after clicking on the Sending button, so in the OnClick method of the button, the interface can be recalled.
Button.setonclicklistener (new View.onclicklistener () { @Override public void OnClick (View v) { = et.gettext (). toString (); Mlistener.fragmentsend (string); } });
Static Loading brief description
The communication between the fragment and activity described above is a dynamic loading process. So how do static loads communicate with each other? In fact, due to static loading, in the activity layout file has fragment related control and ID, you can use Findfragmentbyid to obtain the corresponding fragment, and the set and get method to achieve the data transfer between the two.
Fragmentmanager Supportfragmentmanager == Supportfragmentmanager.findfragmentbyid (R.id.frame); FRAGMENTBYID.SETAAA ("Hello");
Interface display
Android Learning--fragment Communication with activity (ii)