I’d like to call a method that is declared in a class, but the class that contains that method is instantiated by a fragment:
protected void setupViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, AFTab1.class.getName()));
fragments.add(Fragment.instantiate(this, AFTab2.class.getName()));
fragments.add(Fragment.instantiate(this, AFTab3.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}
So for example, if I want to call AFTab1’s method, how can I go about doing that?
There are several options for you. You can store a reference to the fragment returned by
instantiate(), you can give the fragment an id, and you can give it a tag (and I bet there are more options). In your case I would store a reference like this:You can of course store the reference for later use but beware of leakage.
Update: Note that you can create a fragment in many ways as well, for example using its constructor. You don’t have to use
instantiate().