1

I followed http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

It uses the following code to add Tab.

Tab tab = actionBar.newTab()
            .setText(R.string.artist)
            .setTabListener(new TabListener<ArtistFragment>(
                    this, "artist", ArtistFragment.class));
    actionBar.addTab(tab);

I want to supply argument to the fragment's constructor or call myInit(myVariableList) method on the fragment instance before showing the tab for the first time.

How can I do that?

eugene
  • 36,347
  • 56
  • 224
  • 435

1 Answers1

6

You can use tab.setTag() to link an arbitrary object to the tab. If you can put myVariableList into a Bundle, you can achieve a simple solution by doing the following --

Tab tab = actionBar.newTab()
            .setText(R.string.artist)
            .setTabListener(new TabListener<ArtistFragment>(
                    this, "artist", ArtistFragment.class));
    tab.setTag(myVariableBundle);
    actionBar.addTab(tab);

Then, in your onTabSelected callback, send the Bundle when you instantiate your fragment --

mFragment = Fragment.instantiate(mActivity, mClass.getName(), (Bundle) tab.getTag());

You should then be able to access your Bundle during the fragment lifecycle using getArguments()

iagreen
  • 30,052
  • 7
  • 73
  • 88
  • wow nice. Is there a way to retrieve a tab by (class/tag/whatever) and call a method on it as well? – eugene Jan 04 '13 at 06:55
  • In what context do you want to retrieve the tab? It is already available from the creation context (as the variable `tab`) and in the `onTabSelected` method as the parameter `tab`. – iagreen Jan 04 '13 at 07:04
  • i'll ask separate question. Thanks! – eugene Jan 04 '13 at 07:11