Reputation: 1
I am currently having a problem accessing a Fragment ID and Fragment Tag. I am currently implementing a Fragment in a TabHost and ViewPager, the new tab is added like this:
mTabsAdapter.addTab(bar.newTab().setText("Tab0"),
Tab0.class, null);
And the addTab method is the following:
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
I am currently having 2 tabs and 2 fragments opened in my activity: Tab0.class and Tab1.class. I want the fragment Id or Tag of Fragment Tab1 so that I can pass a value to it using callback method. Would you please be so kind to help me out with this ?
I have tried the following without luck:
public String addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
return tab.getTag().toString();
}
and then later I called out the return String as the Fragment tag but it's always null. Could you please help me out ??????? What should I do to access Tab1 Fragment in the background. Thank you so much for your kind help :( !!
Upvotes: 0
Views: 6419
Reputation: 694
I had a similar problem today. I wanted put a value to my fragment and I can't use the Tag because I used a ViewPager and an Adapter. So i used the method getItem() from my adapter that returns the fragment and i create a method in my fragment.
public class SchedulePagerAdapter extends FragmentPagerAdapter {
private final ArrayList<String> mTabs;
public SchedulePagerAdapter(FragmentManager fm, Context mContext) {
super(fm);
mTabs = new ArrayList<String>();
mTabs.add(new String(mContext.getString(R.string.label_monday)));
mTabs.add(new String(mContext.getString(R.string.label_tuesday)));
mTabs.add(new String(mContext.getString(R.string.label_wednesday)));
mTabs.add(new String(mContext.getString(R.string.label_thursday)));
mTabs.add(new String(mContext.getString(R.string.label_friday)));
mTabs.add(new String(mContext.getString(R.string.label_saturday)));
mTabs.add(new String(mContext.getString(R.string.label_sunday)));
}
@Override
public CharSequence getPageTitle(int position) {
return mTabs.get(position);
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
return ScheduleFragment.newInstance(position);
}
}
In the activity I past my value like this:
(FragmentNameClass)mSchedulePagerAdapter.getItem(0).setDate("00-00-00");
Upvotes: 1