Talk Android together (247th time: data transfer between fragments in Android 5)

Hello, ladies and gentlemen, last time we talked about the example of data transfer between fragments in Android, this t...

Hello, ladies and gentlemen, last time we talked about the example of data transfer between fragments in Android, this time we will continue to talk about this example. Don't talk about it. Turn it around. Let's Talk Android!

Dear Sirs, we introduced the use of fragment parameters to transfer data between fragments in the last time. When using this method to transfer data, it is not necessary to distinguish whether fragments are in the same Activity. Only the static method of fragment is needed to create fragment objects. In addition, we can create an interface in fragment and then implement it in the Activity hosting fragment. When the interface is implemented, the data is passed through the static method of Fragment, and then the method in the interface is called in Fragment to complete the data transfer function. The following is the concrete operation steps.

  • 1. Create interface in FragmentB: OnFragmentInteractionListener, which has only one abstract method:
public interface OnFragmentInteractionListener { void onFragmentInteraction(Uri uri); }
  • 2. Implement the interface in ActivityA, and rewrite the abstract methods in the interface to realize the content: transfer data to FragmentA2 and FragmentB;
@Override public void onFragmentInteraction(Uri uri) { Log.i(TAG, "onFragmentInteraction: "); mFragmentA2 = FragmentA2.newInstance("onFragmentInteraction","Data2"); mFragmentB = FragmentB.newInstance("onFragmentInteraction","DataB"); }
  • 3. Create public methods in FragmentB to transfer data using abstract methods in the interface:
public void onButtonPressed(Uri uri) { if (mListener != null) { Log.i(TAG, "onButtonPressed: "); mListener.onFragmentInteraction(uri); } }
  • 4. Call the public method in Fragment to pass the data, and then verify whether the data is passed successfully;
//The public method of Fragment is used to transfer data. The real operation of transferring data is CallBack:onFragmentInteraction mFragmentA2.onButtonPressed(null); mFragmentB.onButtonPressed(null); //Verify that the data was delivered successfully mFragmentA1.getDataFromParam(); mFragmentA2.getDataFromParam(); mFragmentB.getDataFromParam();
  • 5. Initialize the listener in onAttach method of FragmentB, and finally destroy the listener object in onDetach method;
@Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; }

The code above is a bit convoluted, but it implements data transfer between different fragments through an interface, and it can also transfer data between Activity and Fragment. The most important thing is that there is no need to know each other's details between the fragments that transmit data to each other, only the methods in the interface can be used to complete data transmission, so as to reduce the convergence between fragments.

In addition, I want to say that the interface name and public method in the above code are generated automatically when using AST to create Fragment (provided that the check boxes of factory method and interface method are not removed, there is no check box on ast version 4.0, and the interface method and public method are generated by default). It can be seen that this is also the official recommended operation method. I hope you can use this method in the project to complete the data transfer between fragments

Ladies and gentlemen, let's introduce the data transfer between fragments in Android here. If you want to know any more examples, let's listen to the next step!

25 June 2020, 23:51 | Views: 4315

Add new comment

For adding a comment, please log in
or create account

0 comments