1

I have an application like this

enter image description here

In FragmentB I have TextView and one Button. The button's function is replace FragmentB with a new Fragment (FragmentB1). In FragmentB1, I show a ListView with Strings. In listView's onItemClickListener, I have to return the String and put it on FragmentB's TextView. The problem is that I can't use Fragment.startActivityForResult() because I only have one Activity (which manage transactions on the right side of screen and also shows FragmentA). Is it possible to call FragmentB's onActivityResult from this unique Activity?

Note: I can't write a method setText(String text) on FragmentB class because is possible that I will put a FragmentC or FragmentD instead FragmentB

NamNH
  • 1,752
  • 1
  • 15
  • 37
Dani Garcia
  • 464
  • 1
  • 6
  • 18

1 Answers1

0

Dani,

When dealing with fragments, your best means of communication between the fragments is via the activity. For example. Let us consider Activity A that has Fragments B, C, and D. After you set the new Fragment to be placed via FragmentManager, and it is loaded, you can call the getActivity() on it to get a reference to it. A good idea is to implement an interface so that you can use it on any activity that implements it and call the callback method:

// creating the fragment here from the mainActivity  
FragB fragB = new FragB();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.imager, fragB).commit();


// This is because my example activity implements FragBListener which is defined in the FragB class
@Override
public void onCallFromFragB() {

}

FragB

public class FragB extends Fragment {
private FragBListener activity;

....

   public interface FragBListener{
    void onCallFromFragB();

}
}

Now you can reference the parent activity by calling getActivity on onAttach()

public void onAttach(Context context) {
    super.onAttach(context);
....

    activity = (FragBListener) getActivity();
}

Now that you have a reference to the activity, you can call the implemented method from the interface. You can really call any public method from the parent activity really as long as you don't typecast it to the listener, I just prefer it that way.

Now calling a method inside of the Fragment from the activity is even easier. Like so:

FragB fragB= (FragB) getSupportFragmentManager().findFragmentById(R.id.imager);
    if(fragB != null){
        // FragB is in view
        // call it's methods
    }

This way your fragments will be self contained and the communication is done via the activity so you won't really have to worry about the scenario. You just want to make sure that you call the public methods on the fragments as long as they are not null (in the layout).

For reference, you can see this.

Ok so with everything I have mentioned so far, here is what you can do.

  1. Load Fragment B from activity.
  2. When Fragment B's button is clicked. Call the activity's method that replaces Fragment B with Fragment B1.
  3. The onItemClickListener of B1 can then call the activity's method passing in the String so that the activity can replace B1 with B with the TextView set to the String that you just passed in.

If you do not wish to mess around with the interface, you can also just typecast the activity. For example, if the activity is MainActivity,

((MainActivity) getActivity()).[any public method]

is also possible when calling from one of its fragments.

Khanal
  • 788
  • 6
  • 14