I have problem making my fragments communicating with each other through the Activity
, which is using the FragmentPagerAdapter
, as a helper class that implements the management of tabs and all details of connecting a ViewPager
with associated TabHost
. I have implemented FragmentPagerAdapter
just as same as it is provided by the Android sample project Support4Demos.
The main question is how can I get particular fragment from FragmentManager
when I don't have neither Id or Tag? FragmentPagerAdapter
is creating the fragments and auto generating the Id and Tags.
I created this method which is working for me to get a reference to the current fragment.
I don't know if this is the best approach but nothing else worked for me. All other options including getActiveFragment returned null or caused the app to crash.
I noticed that on screen rotation the fragment was being attached so I used it to send the fragment back to the activity.
In the fragment:
Then in the activity:
And finally in activity onCreate():
This approach attaches the actual visible fragment to the activity without creating a new one.