Getting reference to nested fragment from FragmentTabHost

Alex Bonel picture Alex Bonel · Apr 9, 2013 · Viewed 16.9k times · Source

In my application, I use an Activity which holds one Fragment with FragmentTabHost and hence all its tabs are nested Fragments.

Inside an Activity which holds a Fragment with its nested Fragment, we may get a reference to attached one using onAttachedFragment().

But how to get a reference to nested Fragment from FragmentTabHost?

Answer

Alex Bonel picture Alex Bonel · Apr 9, 2013

Well, exploring the source code of FragmentTabHost I've found that when it adds a fragment tab, it assignes a tag of TabSpec to nested Fragment.

So to get the reference to this Fragment we should call

getChildFragmentManager().findFragmentByTag(tabSpecTag)