2

I have a FragmentPagerAdapter, and in the fragment class I have a public method that I need to use from main activity. In this point I don't know how to access the fragment that contains that method. Can I modify something on the class below to set id or something to fragments?

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new Fragment_1();
            case 1:
                return new Fragment_2();
            case 2:
                return new Fragment_3();
            }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.fg_1).toUpperCase(l);
            case 1:
                return getString(R.string.fg_2).toUpperCase(l);
            case 2:
                return getString(R.string.fg_3).toUpperCase(l);
            }
        }
        return null;
    }
}

How can I set a Fragment id, or Fragment tag or something to acces the fragment later?

Any other way to access fragment methods could be good.

EDITED WITH MORE INFORMATION:

I'm really lost with this. But, I have a ViewPager too, these fragments are associated to actionBar Tabs.

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
    actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}

Is there any way to communicate with the fragments?

Jaime Soriano
  • 7,309
  • 2
  • 33
  • 45
osanfer
  • 75
  • 1
  • 7
  • `FragmentPagerAdapter` isn't really well-suited for your use case. Either use some sort of decoupled communications (e.g., an event bus), or switch to a different `PagerAdapter` implementation (e.g., [my `ArrayPagerAdapter`](https://github.com/commonsguy/cwac-pager)). – CommonsWare Sep 22 '15 at 15:35
  • Edited with more information. – osanfer Sep 22 '15 at 15:57
  • Take a look to the answers in this question: http://stackoverflow.com/questions/14035090/how-to-get-existing-fragments-when-using-fragmentpageradapter – Jaime Soriano Sep 22 '15 at 16:31

1 Answers1

-2

Like so :

        switch (position) {
        case 0:
            Fragment f = new Fragment_1();
            f.setId(...);
            return f;
        case 1:
            ...
        }

This assumes you have a setId method in your fragment.

What I do however is define a static string TAG per fragment, I can then access it with Fragment_1.TAG.

ElDuderino
  • 3,253
  • 2
  • 21
  • 38
  • 2
    There is no `setId()` method on `Fragment`. – CommonsWare Sep 22 '15 at 15:34
  • The question was : "Can i modify something on the class below to set id or something to fragments?" ... The answer is yes, he just has to create the method. Not sure what he wants to do with the Id. – ElDuderino Sep 22 '15 at 15:37
  • 1
    "Not sure what he wants to do with the Id" -- call `findFragmentById()`, presumably. "What I do however is define a static string TAG per fragment, I can then access it with Fragment_1.TAG" -- since the `FragmentPagerAdapter` subclass is not executing the `FragmentTransaction`, there is no opportunity to set the tag for the fragment. – CommonsWare Sep 22 '15 at 15:39