-3

Consider an activity we can call as a base activity. Two fragments are added to this base activity , call as fragmentOne and fragentTwo.How can fragmentOne can communicate with fragentTwo and vice versa .

jobi mg
  • 749
  • 6
  • 12

3 Answers3

0

You can define interface to communicate between fragments. go through this link http://developer.android.com/training/basics/fragments/communicating.html hope it helps.

inkedTechie
  • 684
  • 5
  • 13
0

refer Communicating with Other Fragments,the communication among fragments should by activity,we'd better don't use directly communicate.We must know fragment lifecycle must depend on activity,and considering coupling of classes,we should let fragment do own task and other tasks need to communicate with other class should by its holder activity.

starkshang
  • 8,228
  • 6
  • 41
  • 52
0

All communication between fragments should be implemented through the activity. For example, if you want to call method foo() inside Fragment1 from Fragment2, add intermediary method to activity, which contain both fragments (for example MainActivity):

public void foo(){
    Fragment f = getFragmentManager().findFragmentById(R.id.fragment1);
    if (f != null) {
        ((Fragment1) f).foo();
    }
}

Now you can call it from Fragment2: ((MainActivity)getActivity()).foo()

But don't forget, that all fragments have own lifecycles.

Denys Vasylenko
  • 2,135
  • 18
  • 20