Fragment and activity transmit data to each other as follows:
1. Activity transmits data to fragment: Create bundle packets in activity (if the transmission involves objects, implement serialization of objects), and call fragment's setarguments (bundle Bundle) method can be implemented to pass bundle packets to fragment, which is easy to implement.
2, but if the reverse, we click on the trigger fragment event, we want to return the data activity,activity to get the data after the operation, that is, fragment actively transfer data to the activity, which is also this blog will be explained in detail, The interface callback is used to realize the communication between fragment and activity.
Now suppose a scene: If Fragment1 is a window showing some news, now we need to click on the News window (This news window is an Android component, the following code assumes it is a button and binds it to the corresponding URL), Return the corresponding UiR activity,activity get the news window information URL that the user clicked, then call the specific news Fragment2 to display the specific news (pass the URL to Fragment2, let the Fragment2 load the specific news).
The core implementation of FRAGMENT1:
Class Fragment1 extends Fragment {------button button;//Bind button to Web site Urlbutton.settext ("www.baidu.com");------ --------Onmycliklistener mylistener;//definition Callback Interface interface Onmycliklistener {public void Onclik (View v);} Defines the function that is called by the activity public void Setoncliklistener (Onmycliklistener onmycliklistener) {This.mylistener = Onmycliklistener;} Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stub
Here the clicked News Window (v) is passed to Activitymylistener.onclik (v);
System.out.println ("and Me");}); - - - - - - -- - - - - - -- - - - - - -}
The core implementation of activity
Class MyActivity extends Activity {------//define FRAGMENT1, then add fragment to activity, omit Fragment1 fragment;-here------- ------//calls the public function of fragment in activity, which we have defined above Fragment.setoncliklistener (new Onmycliklistener) {@ overridepublic void OnClick (View v) {//TODO auto-generated method stub//Note that this parameter v is actually a handle to the button in the Fragment1, It allows us to access the button bound Urlbutton button = (button) v; String URL = button.gettext (). toString ();}} - - - - - - -- - - - - - -- - - - - - -- - - - - - -}
Combing the execution sequence
1, first, when we click on Fragment1 inside the button, the execution of Fragment1 inside the part of this function
Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//is here to pass the clicked News Window (v) to Activitymylistener.onclik (v);
System.out.println ("and Me");
}});
That is, execute
public void OnClick (View v)
function, but when executing to the
Here the clicked News Window (v) is passed to Activitymylistener.onclik (v);
In this sentence, the interface function that defines the interface is called and V is used as the argument, and this v is the handle to the button that is clicked
Let's see, the implementation of this interface function is implemented in the activity and then goes to the activity
2, the above is said to carry out to the activity, that is, the activity of this part of the function
In the activity call fragment's public function, this function we have defined above Fragment.setoncliklistener (new Onmycliklistener) {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//Note that this parameter v is actually a handle to the button in the Fragment1, It allows us to access the button bound Urlbutton button = (button) v; String URL = button.gettext (). toString ();}}
Executing here, the activity obtains the URL which the Fragment1 passes over, realizes the Fragment1 actively transmits the data to the activity
When this function is done, and then back to 1</span>
Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//is here to pass the clicked News Window (v) to Activitymylistener.onclik (v);
System.out.println ("and Me");
}});
3, back to 1, continue to execute
System.out.println ("and Me");
That is, as shown
Anyway
1, the user Fragment1 inside of the button click event, the implementation of the Fragment1 the data URL to the initiative to activity,activity to obtain the URL, you can do later operations
2, in fact, the reader has not felt the interface callback is much like the way the component listener, yes, the Android Component Listener interface is also the callback interface, but also the implementation of
Using interface callback to realize fragment communication with activity