0

Good day, you awesome people! Here's my case. I want to refresh the contents of a nested fragment based on an option from the ActionBar. The fragment has a ListView and should display one list if the first ActionBar option is selected and another list if the second option is selected. Let's say I have couple of options in my ActionBar ("All" and "Highlighted").

enter image description here

The desired fragment is in a TabHost, which has a ViewPager.

<TabHost>

<LinearLayout>

    <TabWidget
        android:id="@android:id/tabs" />

    <FrameLayout
        android:id="@android:id/tabcontent" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager" />

</LinearLayout>

In addition, this is how I add a tab:

mHomeTabsAdapter.addTab(mTabHost.newTabSpec("controls").setIndicator("CONTROLS"), ControlsFragment.class, null);

Since I'm adding the fragment programatically, I cannot give it an id and use the FragmentManager to find it by ID. How do I get a reference to ControlsFragment so I can refresh it from MainActivity's onNavigationItemSelected?

In the ideal situation, I should be able to get a reference to the fragment within onNavigationItemSelected and reinstantiate the adapter that provides the list with items.

I read the communication article (http://developer.android.com/training/basics/fragments/communicating.html) but I'm afraid it's not going to work for me. Anyone got suggestions?

Kiril Stanoev
  • 1,865
  • 2
  • 20
  • 33

1 Answers1

0

Use a tag!

When you do a transaction you can specify a tag.

 FragmentTransaction.add(Fragment fragment, String tag);

You can find the fragment with

 FragmentManager.findFragmentByTag(String tag);
ElDuderino
  • 3,253
  • 2
  • 21
  • 38
  • Unfortunately I'm not doing transactions. I'm using a FragmentStatePagerAdapter to sync and feed items to the TabHost and ViewPager. Internally, the adapter gives me the proper fragment in the following manner: @Override public Fragment getItem(int position) { TabInfo info = mTabs.get(position); Fragment fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args); return fragment; } – Kiril Stanoev Jan 16 '14 at 13:47