Use the interface callback to implement the communication between fragment and activity.

Source: Internet
Author: User

Use the interface callback to implement the communication between fragment and activity.

Fragment and activity transmit data to each other as follows:

1. The Activity transmits data to Fragment: Creates a Bundle data packet in the Activity (if the transmission involves an object, serialization of the object is required), and CALLS setArguments (Bundle bundle) of Fragment) this method can be used to send Bundle data packets to Fragment, which is easy to implement.

2. But if, in turn, when we click to trigger the Fragment event, we want to return the data to the Activity. The Activity obtains the data for subsequent operations, that is, Fragment actively transmits data to the Activity, this is what will be explained in detail in this blog post. You can use the interface callback to implement the communication between fragment and activity.


Now let's assume that Fragment1 displays some News windows. Now we need to click the news window (this news window is an Android component, the following code assumes that it is a Button and binds it to the corresponding URL.) return the corresponding uir to Activity and Activity to get the URL of the news window information clicked by the user, then call Fragment2 of the specific news to display the specific News (send the URL to Fragment2 and let Fragment2 load the specific News ).


Core Implementation of Fragment1:

[Java]View plaincopy
  1. Class Fragment1 extends Fragment {
  2. -------
  3. Button button;
  4. // Bind the button to the website url
  5. Button. setText ("www.baidu.com ");
  6. -------
  7. -------
  8. OnMyclikListener myListener;
  9. // Define the callback Interface
  10. Interface OnMyclikListener {
  11. Public void onclik (View v );
  12. }
  13. // Define the function for the Activity to call
  14. Public void setOnclikListener (OnMyclikListener onMyclikListener ){
  15. This. myListener = onMyclikListener;
  16. }
  17. Button. setOnClickListener (new OnClickListener (){
  18. @ Override
  19. Public void onClick (View v ){
  20. // TODO Auto-generated method stub
[Java]View plaincopy
  1. // Here, the clicked news window (v) is passed to the Activity
[Java]View plaincopy
  1. System. out. println ("and me ");
  2. }
  3. });
  4. -------
  5. -------
  6. -------
  7. }
[Java]View plaincopy
  1. Core Implementation of Activity
[Java]View plaincopy
  1. Class MyActivity extends Activity {
  2. -------
  3. // Define Fragment1 and add Fragment to Activity.
  4. Fragment1 fragment;
  5. -------
  6. -------
  7. // Call the Fragment public function in the Activity. This function is defined above.
  8. Fragment. setOnclikListener (new OnMyclikListener ){
  9. @ Override
  10. Public void onClick (View v ){
  11. // TODO Auto-generated method stub
  12. // Note that this parameter v is actually the handle of the button in Fragment1. We can use it to access the url bound to the button.
  13. Button button = (Button) v;
  14. String url = button. getText (). toString ();
  15. }
  16. }
  17. -------
  18. -------
  19. -------
  20. -------
  21. }


 
 
 
[Java]View plaincopy 
  1. Sort execution order
[Java]View plaincopy
  1. 1. First, when we click the button in Fragment1, execute one of the functions in Fragment1.
[Java]View plaincopy
  1. Button. setOnClickListener (new OnClickListener (){
  2. @ Override
  3. Public void onClick (View v ){
  4. // TODO Auto-generated method stub
  5. // Here, the clicked news window (v) is passed to the Activity
  6. MyListener. onclik (v );
[Java]View plaincopy
  1. System. out. println ("and me ");
[Java]View plaincopy
  1. }
  2. );
That is, execute

Public void onClick (View v)

Function, but when

[Java]View plaincopy
  1. // Here, the clicked news window (v) is passed to the Activity
  2. MyListener. onclik (v );
[Java]View plaincopy
  1. In this statement, the interface function of the defined interface is called and v is used as a parameter. This v is the handle of the clicked button.
[Java]View plaincopy
  1. Let's take a look at the implementation of this interface function, which is implemented in the Activity, and then executed to the Activity
[Java]View plaincopy
  1. 2. As mentioned above, the execution is transferred to the Activity, that is, the function of the Activity.
[Java]View plaincopy
  1. // Call the Fragment public function in the Activity. This function is defined above.
  2. Fragment. setOnclikListener (new OnMyclikListener ){
  3. @ Override
  4. Public void onClick (View v ){
  5. // TODO Auto-generated method stub
  6. // Note that this parameter v is actually the handle of the button in Fragment1. We can use it to access the url bound to the button.
  7. Button button = (Button) v;
  8. String url = button. getText (). toString ();
  9. }
  10. }
[Java]View plaincopy
  1. After the execution, the Activity obtains the Url passed by Fragment1, enabling Fragment1 to actively pass data to the Activity.
[Java]View plaincopy
  1. After this function is executed, return to 1 </span>
[Java]View plaincopy
  1. Button. setOnClickListener (new OnClickListener (){
  2. @ Override
  3. Public void onClick (View v ){
  4. // TODO Auto-generated method stub
  5. // Here, the clicked news window (v) is passed to the Activity
  6. MyListener. onclik (v );
[Java]View plaincopy
  1. System. out. println ("and me ");
[Java]View plaincopy
  1. }
  2. );

3. Return to 1 and continue execution

As shown in

 
[Java]View plaincopy 
[Java]View plaincopy
  1. In short
[Java]View plaincopy
  1. 1. The button click event in Fragment1 enables Fragment1 to actively pass the Data Url to the Activity. After the Activity obtains the url, it can perform subsequent operations.
[Java]View plaincopy
  1. 2. Does the reader actually feel that the interface callback method is similar to the component listener method? Yes, the Android component listening interface is also the callback interface, which is also implemented in this way.

Related Article

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.