Reputation: 546
I am developing an app with action bar sherlock. In Sherlock fragment activity i added two fragments
public class My Project extends SherlockFragmentActivity implements ActionBar.TabListener{
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
setContentView(R.layout.main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("My Project");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MainSettinngs.class.getName()));
fragments.add(Fragment.instantiate(this, OtherSettings.class.getName()));
// Create the adapter that will return a fragment for each of the two
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),fragments,this);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText(getString(R.string.title_main_settings)).setTabListen(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.title_other_settings)).setTabListener(this));
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
}
and SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
Context _context;
public SectionsPagerAdapter(FragmentManager fm,List<Fragment> fragments,Context context) {
super(fm);
this.fragments = fragments;
_context = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
With these i have two fragments MainSettings and OtherSettings which extends support fragment class.
In my Sherlock fragment activity i want current objects of the MainSettings and OtherSettings so that i can
Upvotes: 1
Views: 3071
Reputation: 546
I got the answer.
The Fragments supplied by the FragmentPagerAdapter are auto-tagged when they're instantiated.
So to get the tag you have to use "android:switcher:" + mViewPager.getId() + ":" + position
//this will give you instance of the fragment at position 0
Fragment currentFragmentMainSettings = getSupportFragmentManager().findFragmentByTag( "android:switcher:" + mViewPager.getId() + ":" + 0);
//Now call the method of your fragment
((MainSettinngs) currentFragmentMainSettings).resetMainSettings();
Fragment currentFragmentOtherSettings = getSupportFragmentManager().findFragmentByTag( "android:switcher:" + mViewPager.getId() + ":" + 1);
((OtherSettings) currentFragmentOtherSettings).resetOtherSettings();
Upvotes: 2
Reputation: 6616
Try something like this
public enum TabEnum
{
NONE, MAIN, OTHER;
public static TabEnum fromInt(int id)
{
switch (id)
{
case 0:
return MAIN;
case 1:
return OTHER;
}
return NONE;
}
public int toInt()
{
switch (this)
{
case MAIN:
return 0;
case OTHER:
return 1;
case NONE:
default:
return -1;
}
}
};
public FragmentMain getFarFragmentMain()
{
return ((FragmentMain) (getViewPager().getViewPagerAdapter().instantiateItem(getViewPager(), TabEnum.MAIN.toInt())));
}
public FragmentOther getFarFragmentOther()
{
return ((FragmentOther) (getViewPager().getViewPagerAdapter().instantiateItem(getViewPager(), TabEnum.OTHER.toInt())));
}
These will return the correct instances of your fragments which you can later use as
getFarFragmentMain().myMethodFromMain(arg1, arg2);
Upvotes: 1