Android two fragment transfer data between examples of detailed _android

Source: Internet
Author: User

How to pass data between Android two fragment

Fragmenta start FRAGMENTB, do some select operation, return to Fragmenta, need to FRAGMENTB inside the selection of the data sent back. What's the solution?

Fragment can not communicate directly between the two, must be done through the activity, the specific steps.

1. Define a communication interface in Fragmenta that sends data to the activity through this interface.

 public class Fragmenta extends Fragment {private Onbuttonpresslistener mlistener; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V
    Iew = Inflater.inflate (R.layout.fragment_linmo_select_beitie, container, false); Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (ADAP
      Terview<?> Parent, view view, int position, long id) {mlistener.onokbuttonpressed (selectedbeitie);

    }
    });
  return view;
    @Override public void Onattach (activity activity) {Super.onattach);
    try {Mlistener = (onbuttonpresslistener) activity; catch (ClassCastException e) {throw new ClassCastException (activity.tostring () + "must implement Onokbuttonpress
    Ed ");
  } public interface Onbuttonpresslistener {void onokbuttonpressed (Linmobeitieitem item); }
}

2. Implement the interface in the activity and pass the data to the FRAGMENTB through the interface.

public class Mainactivity extends activity implements Fragmenta.onbuttonpresslistener {
  @Override public
  Void Onokbuttonpressed (Linmobeitieitem Item) {
    FRAGMENTB fragmentb = (FRAGMENTB) Getfragmentmanager (). Findfragmentbyid (R.id.container);
    fragmentb.onbeitieselected (item);
  }

3. FRAGMENTB received the data and processed it.

public class Fragmenta extends Fragment {public
  void onbeitieselected (Linmobeitieitem item) {
    //...
  }
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.