0

I have a Fragment which displays content for my app, but I also have a Spinner below the Fragment.

I want something to happen in the Fragment when the value of the Spinner is changed. So my question is how does the MainActivity communicate with the Fragment?

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Chirag Galaiya
  • 87
  • 1
  • 1
  • 10

1 Answers1

2

For the MainActivity to communicate with one the Fragments in its FragmentManager, you'd access it with either findFragmentById() or findFragmentByTag(). I.e.

MyFrag frag = getFragmentManager().findFragmentById(R.id.frag_container);

or

getFragmentManager().findFragmentByTag("my_frag_tag");
PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • What about if it is in a ViewPager? @PPartisan – Chirag Galaiya Mar 29 '16 at 19:49
  • @ChiragGalaiya Then that's a bit trickier. You can use the method outlined in my answer [here](http://stackoverflow.com/questions/35043469/controlling-back-button-inside-fragment/35044012#35044012) (also [here](http://stackoverflow.com/questions/34182919/call-fragment-function-over-activity/34183721#34183721)) for an approach that involves cycling through every `Fragment` in your `FragmentManager`, or use an `EventBus` – PPartisan Mar 29 '16 at 19:53