SHARE
TWEET

Android Fragment Manager Example

m1nd Mar 2nd, 2015 (edited) 786 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     private class MyPagerAdapter extends FragmentPagerAdapter {
  2.         private FragmentManager fm;
  3.  
  4.         public MyPagerAdapter(FragmentManager fm) {
  5.             super(fm);
  6.             this.fm = fm;
  7.         }
  8.  
  9.         @Override
  10.         public Fragment getItem(int pos) {
  11.             Fragment fragment = fm.findFragmentByTag("android:switcher:" + viewPager.getId() + ":" + getItemId(pos));
  12.  
  13.             // Return the fragment if it was stored in the fragment manager
  14.             if (fragment != null) {
  15.                 return fragment;
  16.             }
  17.            
  18.             // Get a new instance of the target fragment
  19.             switch(pos) {
  20.             case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
  21.             case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
  22.             case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
  23.             case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
  24.             case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
  25.             default: return ThirdFragment.newInstance("ThirdFragment, Default");
  26.             }
  27.         }
  28.  
  29.         @Override
  30.         public int getCount() {
  31.             return 5;
  32.         }      
  33.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top