0

Let us say we have two fragments A and B, We send an object O1 from fragment A to B using Bundle and bring fragment B into the current activity.Do some changes with O1 i.e store data in payload object O1 and now we come back to fragment A where the save button is placed. Now if we save the data which O1 has i.e O1.savetoDatabase() We don't get the data changes we made in fragment B. Is there a possible way to get the changes in the fragment A for the object passed to fragment B? (something like the references which inflicts the changes)

EDIT: The answer two which uses setTargetFragment seems to work but the only problem is when I am on fragment B and then send data to fragment A, I am receiving the data in the object O1(this works fine) of fragment A but when I press the back button from fragment B to fragment A, Data in O1 is lost due to fragment recreation. What should be done to persist the data sent?

Falcon
  • 372
  • 4
  • 20
  • **[Share data between fragments](https://developer.android.com/topic/libraries/architecture/viewmodel)** – AskNilesh Jun 26 '18 at 04:21

3 Answers3

0

Send modified O1 to fragment A using Bundle. Or you can use Eventbus to observe changes https://github.com/greenrobot/EventBus

Harry T.
  • 3,478
  • 2
  • 21
  • 41
0

You can use setTargetFragment().Create the getter and setter for your object.

public class FragmentA extends Fragment {
    private String mName;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentB fragmentB = new FragmentB();
        fragmentB.setTargetFragment(this, 1111);
    }

    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }
}

public class FragmentB  extends Fragment {
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ((FragmentA)getTargetFragment()).setmName("xyz");
    }
}

Or You can use onActivityResult method to send the data via intent like below from fragmentB to FragmentA.

getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent)
Mahavir Jain
  • 1,448
  • 10
  • 23
  • Can you please check the edit in the question. That explains the problem I am facing – Falcon Jun 26 '18 at 10:31
  • You can use one boolean variable to check if the value of object is changed or not and write your code inside onDestroyView(). `@Override public void onDestroyView() { super.onDestroyView(); if (getTargetFragment() != null && isUpdated) ((FragmentA) getTargetFragment()).setmName("xyz");}` And if you replacing the fragment each time and not adding the fragment in back-stack than either you have to save the data in shared preference or you have to create the Object in your activity class so that both of the fragment can use same object @Falcon – Mahavir Jain Jun 26 '18 at 14:13
0

The recommended way to communicate between fragments is to create a shared ViewModel object. Both fragments can access the ViewModel through their containing Activity. The Fragments can update data within the ViewModel and if the data is exposed using LiveData the new state will be pushed to the other fragment as long as it is observing the LiveData from the ViewModel. To see how to implement this kind of communication, read the 'Share data between Fragments' section in the ViewModel guide.

ref. - https://developer.android.com/training/basics/fragments/communicating

ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment. It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).

A ViewModel is always created in association with a scope (an fragment or an activity) and will be retained as long as the scope is alive. E.g. if it is an Activity, until it is finished.

In other words, this means that a ViewModel will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation). The new instance of the owner will just re-connected to the existing ViewModel.

The purpose of the ViewModel is to acquire and keep the information that is necessary for an Activity or a Fragment. The Activity or the Fragment should be able to observe changes in the ViewModel. ViewModels usually expose this information via LiveData or Android Data Binding. You can also use any observability construct from you favorite framework

ref. - https://developer.android.com/reference/android/arch/lifecycle/ViewModel

check this too

Sharing data between fragments using new architecture component ViewModel

hope this helps

Anjani Mittal
  • 507
  • 1
  • 7
  • 19