Calling a method with tabs from a fragment

I have one action that consists of three fragments. Snippets use action bar tabs using the PagerAdapter. What I want to do is access the method in the active fragment with tabs from the main action. I tried the code below, but it just returns the snippet as null, so I think it cannot find it inside the tabs!

NPListFragment articleFrag = (NPListFragment) getSupportFragmentManager().findFragmentByTag("NP"); articleFrag.refreshT(); 

Pageradapter:

 public class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: return new NPListFragment(); case 1: return new PListFragment(); case 2: return new FavouritesFragment(); } return null; } @Override public int getCount() { return 3; } 

} Can anyone advise? I spent about 6 hours on it, I just do not make any progress in resolving this.

+5
source share
3 answers

What you need to do: create only each fragment once, and then pass it on for all calls to the getItem method.

For instance:

 public class AppSectionsPagerAdapter extends FragmentPagerAdapter { Fragment one, two, three; public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: if(one == null) one = new NPListFragment(); return one; case 1: if(two == null) two= new PListFragment(); return two; case 2: if(three == null) three= new FavouritesFragment(); return three; } return null; } @Override public int getCount() { return 3; } } 

Now, even in your activity you can call getItem

You just need to pass it to the real fragment class if you want to call a specific method.

 int pos = viewpager.getCurrentItem(); Fragment activeFragment = adapter.getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).refreshT(); ... 
+13
source

You need to register your fragments during filling, and then access them from the outside.

 public class AppSectionsPagerAdapter extends FragmentPagerAdapter { private SparseArray<Fragment> registeredFragments = new SparseArray<>(); public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: return new NPListFragment(); case 1: return new PListFragment(); case 2: return new FavouritesFragment(); } return null; } @Override public int getCount() { return 3; } @Override public Object instantiateItem(ViewGroup container, int position) { Fragment fragment = (Fragment) super.instantiateItem(container, position); registeredFragments.put(position, fragment); return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { registeredFragments.remove(position); super.destroyItem(container, position, object); } public Fragment getRegisteredFragment(int position) { return registeredFragments.get(position); } 

Now you can register fragments from the outside

 int pos = viewpager.getCurrentItem(); Fragment fragment = adapter.getRegisteredFragment(pos); switch (pos) { case 0: { ((NPListFragment) fragment).refreshT(); break; } case 1: { ((PListFragment) fragment).refreshK(); break; } case 2: { ((FavouritesFragment) fragment).refreshL(); break; } } 
+2
source

I solved this problem.

This is because the Adapter is initialized each time a new fragment is called, when the item element is called.

To solve this problem, you need to create an array of the required fragment in the onCreate method

 Fragment[] FragmentArray; FragmentArray = new Fragment[] {new Fragment 1,new Fragment 2...}; // all fragment goes in array ... get item(int position) ... return FragmentArray[position] ... Fragment1 f= (Fragment1) FragmentArray[0]; f.refresh() // call required function 
+1
source

All Articles