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!