1. Use the Fragment Result API to pass data

Starting with Fragment 1.3.0-Alpha04, each FragmentManager implements the FragmentResultOwner interface. This means that FragmentManager can act like a repository for communication between fragments. This also eliminates the need for direct references for Fragment communication.

To pass data from FragmentB to FragmentA, you first set up a listener for FragmentA

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getParentFragmentManager().setFragmentResultListener("key".this.new FragmentResultListener() {
        @Override
        public void onFragmentResult(@NonNull String key, @NonNull Bundle bundle) {
            // String is used here, but any other data types that can be placed in the Bundle are supported
            String result = bundle.getString("bundleKey");
            // Do something else}}); }Copy the code

In FragmentB, the result is produced. Note that FragmentB must use the same FragmentManager as FragmentA, using the same requestKey

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bundle result = new Bundle();
        result.putString("bundleKey"."result");
        getParentFragmentManager().setFragmentResult("requestKey", result); }});Copy the code

Once FragmentA is STARTED, it will receive the result and perform a listening callback.

2. Points needing attention:

  1. There can only be one listener and one result for the same key. If you call it multiple timessetResult()Method, the system will send FragmentA the most recent result.
  2. If a result is set and no listener receives it, the result will be stored in theFragmentManagerUntil the listener is set with the same key
  3. The Fragment listening for the result must be locatedSTARTEDState to accept the result
  4. Once a listener receives a result and executes itonFragmentReultCallback, then the result is consumed.

3. Pass data between parent Fragment and child Fragment

In order to transfer data from sub fragments to father Fragment, when calling setFragmentResultListener (), ParentFragment should use getChildFragmentManager() instead of getParentFragmentManager()

Other fragments pass data in line with the level of fragments.

4. Other communication modes between Fragments

  • You can do this by sharing the ViewModel
  • Interface through: Define interface in Fragment and implement it in Activity; If an Activity wants to communicate with a Fragment, it can use the findFragmentById method, get an instance of the Fragment, and then call the Fragment’s public method
  • throughFragment.setTargetFragment()andFragment.getTargetFragment()methods

5. To summarize

Using the Fragment Result API to communicate between Fragments also has its drawbacks. The seriality-like nature of using bundles means that it is much less flexible than using interfaces. It does not allow callbacks to functions via interfaces.

Reference:

Google-pass data between fragments

Google-communicate with other fragments