Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Passing data from child fragment to parent fragment

I need to pass some data from the child fragment to the parent fragment that I will be able to read when I go back to the parent fragment. In detail:

I have a FragmentActivity that calls FragmentParent. From FragmentParent I call FragmentChild like this:

FragmentChild fragmentChild = new FragmentChild();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
ctransaction.commit();

In FragmentChild I set a string value which I need to pass back to FragmentParent and then I return back to FragmentParent.

String result = "OK";
getFragmentManager().popBackStack();

What is the best/proper way to read the result string in FragmentParent?

like image 293
goseib Avatar asked Apr 15 '14 08:04

goseib


People also ask

How can I transfer data from one fragment to another fragment in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken fragments to pass the data between two fragments.

How can we pass data from fragment to fragment?

Passing Data between fragments in Android using ViewModel: To actually pass the data between fragments, we need to create a ViewModel object with an activity scope of both the fragments, initialize the ViewModel , and set the value of the LiveData object.

How do you pass data from second fragment to first fragment?

Fragments were made to plug and play anywhere. All you have to do is implement the correct type of listener, but the main point is shown. In one fragment activity, call a method and pass a variable to the main activity. From the main activity you can send it to your other fragment activity if you'd like.

How do you pass data between bundles using fragments?

Steps for Sending a Bundle to a Fragment The bundle will be saved using a key/value pair, so in MainActivity. java create a String variable for the key. Create a new bundle. Use the appropriate method from the Bundle class to send your bundle.


1 Answers

Android architecture components solution:

In case you are using Android architecture components, it possible to share data between all Fragments of an Activity with a ViewModel. Ensure ViewModelProviders makes use of Activity context to create ViewModels.

public class SharedViewModel extends ViewModel {
    private final MutableLiveData<Item> selected = new MutableLiveData<Item>();

    public void select(Item item) {
        selected.setValue(item);
    }

    public LiveData<Item> getSelected() {
        return selected;
    }
}

public class MasterFragment extends Fragment {
    private SharedViewModel model;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
        itemSelector.setOnClickListener(item -> {
            model.select(item);
        });
    }
}

public class DetailFragment extends Fragment {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
        model.getSelected().observe(this, { item ->
           // Update the UI.
        });
    }
}

Non Android architecture components solution:

You can use setTargetFragment and onActivityResult to achieve this.

Set FragmentParent instance as target fragment on FragmentChild instance i.e.

FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setTargetFragment(this, FRAGMENT_CODE);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
transaction.commit();

In FragmentChild, wherever you are invoking the popBackStack, call onActivityResult on the set target Fragment. Use Bundle to pass on additional data.

Intent intent = new Intent();
intent.putExtra(FRAGMENT_KEY, "Ok");
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
getFragmentManager().popBackStack();

Back in FragmentParent, override the default onActivityResult method.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
        if(data != null) {
           String value = data.getStringExtra(FRAGMENT_KEY);
           if(value != null) {
              Log.v(TAG, "Data passed from Child fragment = " + value);
           }
        }
    }
}  
like image 174
Manish Mulimani Avatar answered Sep 19 '22 09:09

Manish Mulimani