I'm having trouble getting a pointer to a Fragment which is the currently visible fragment in a FragmentTabhost.
I have a SherlockFragmentActivity
called SecondActivity
that loads the Tabhost from it's onCreate
method like this:
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
Fragment f = new TabsFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, f, "tabsfragment").commit();
}
TabsFragment
is a SherlockFragment
subclass with this onCreate
method to create the tabs
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.tabs);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Offers",
getResources().getDrawable(R.drawable.offersale)),
OfferListFragment.class,
null);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("News",
getResources().getDrawable(R.drawable.newspaper)),
NewsFragment.class,
null);
return mTabHost;
}
Now when i'm in the 2nd tab, I have a background task done in a class that is initiated by the original activity SecondActivity
, then I call this which is supposed to give me a reference to the tab, but it always returns null!
NewsFragment newsView = (NewsFragment) delegate.getSupportFragmentManager().findFragmentByTag("Tab2");
The delegate
variable is a pointer back to SecondActivity
when it starts the background class.
How do I get a pointer to the tab's fragment?
Am I wrong that "Tab2" set when adding the tabs is the Tag
for the fragment?
I don't really like answering my own questions, but it's amazing what sleeping on it can do. This monster gives me a pointer to the fragment in the tabhost
NewsFragment newsView = (NewsFragment) delegate
.getSupportFragmentManager()
.findFragmentByTag("tabsfragment")
.getChildFragmentManager()
.findFragmentByTag("Tab2");