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?



Android – Passing data from fragment to activity?

Can you give me an example of passing item in listview from fragment class to activity’s listview? appreciate if someone help me thanks!

android fragment – data passing – best practice [duplicate]

This question already has an answer here: Best practice for instantiating a new Android Fragment 4 answers When passing data to a fragment why do this – (i.e. the way you are supposed to do it)

Passing data from one fragment to next fragment in android

In one of my project I am using 2 fragments.One fragment is displaying a list of items.So I want when the topic will be selected in first fragment the details should be changed in another fragment.Dat

Passing values from activity to fragment android

I want to pass certain values from an activity to a fragment in android. Here is some code:- MainActivity:- PlaceDialogFragment dialogFragment = new PlaceDialogFragment(place,dm); Fragment:- public

passing data from fragment to activity

I have an activity that contains a fragment. On a click on the actionbar it should create a new fragment with a sting[] from the first activity. I pass the data to the new fragment with setArguments()

Child fragment replacing parent fragment root layout

I’ve viewpager with 5 fragments, in one of them I want to completely replace it by button click. I also want to be able to hide child fragment by back button. Here this fragment layout: <?xml vers

Passing data from fragment in an Activity from Another Activity

I have researched on passing data from Activity to Activity. But I could not use the same method to pass to my fragment. How do I pass the data to my fragment? The Code I am using is Intent i = new I

Access ViewPager’s Parent Fragment View in child fragment of Pager

I want to access a parent Fragment views in a child fragment which is attached with a View Pager. Means Parent Fragment contains a View Pager and View Pager then have a Fragment, and I want to use Par

Remove Child View From Fragment

I’m on Android 2.3.3 and I’m trying to remove programmatically a child view node from its container. I’m inside a fragment. The method removeView doesn’t seem to work… The method getCategories is an

Passing parameters from Android FragmentActivity to Fragment

When Im trying to pass a parameter from FragmentActivity to a Fragment it gives me null pointer exception in the getArguments() in the Fragment. Here is my FragmentActivity Code public class IndexCha

Answers

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);
ctransaction.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 = intent.getStringExtra(FRAGMENT_KEY);
           if(value != null) {
              Log.v(TAG, "Data passed from Child fragment = " + value);
           }
        }
    }
}