2

I have two Fragments A and B. I am going from fragment A to Fragment B using Following method.

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new B()).addToBackStack(TAG).commit();

Now I want to send data to from fragment B to Fragment A i.e Previous Fragment. Can you please give me an idea about how to send data to previous Fragment.

Thanks

  • declare a static variable in Fragment A and Assign it value in B,so that you can use it when you go back to A – Rajan Kali Sep 21 '15 at 07:34
  • http://stackoverflow.com/a/12105615/5069663 May be this answer is useful. Just go through link. – Asmi Sep 21 '15 at 07:36
  • 2
    possible duplicate of [How to pass result from second fragment to first fragment](http://stackoverflow.com/questions/12103953/how-to-pass-result-from-second-fragment-to-first-fragment) – Jaiprakash Soni Sep 21 '15 at 07:43

3 Answers3

0

Check the documentation about Fragments and its communication.

Do you want to pass info from one fragment to another? One fragment shouldn't know about other fragments. Make the fragment B communicate with its activity through an interface and make the activity pass the data to fragment A

Alberto S.
  • 7,409
  • 6
  • 27
  • 46
0

You can communicate between fragments through Activity where you are replacing fragments.

  1. Pass data to activity and then call method of that fragment where you want to update data. 2.Other way you can take a variable in Activity and save the data to this variable and when another fragment (Fragment A) is loaded(Resumed) then show data from Activity variable by getting

    ((HomeActivity)getActivity).variableName;

Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32
-1

You create a subclass of Fragment called MyFragment that A, B are instances of it. Then create a static factory method take a MyFragment as param like:

public static MyFragment newInstance(MyFragment a){
    MyFragment b = new MyFragment();
    MyFragment b.a = a;
    return b;
}

b.a is a field of MyFragment. Then you can use reference a to send data.

mr.icetea
  • 2,607
  • 3
  • 24
  • 42