Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager fragments disappear when change screen rotation

I'm working on android application that contains ViewPager with Fragments, like this:

MainActivity(MainActivityFragment(screenSlideViewPager(Fragments))), which means:

Activity contains Fragment contains ViewPager contains Fragments

Now my problem is when rotate device or change screen rotation all Fragments in ViewPager are disappear.

any ideas to solve this issue?

EDIT 1

MainActivity.java:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {         final FragmentTransaction ft = getSupportFragmentManager()                 .beginTransaction();         ft.add(android.R.id.content, new MainActivityFragment(), TAG);         ft.commit();     } } 

MainActivityFragment.java:

public class MainActivityFragment extends Fragment {     private ViewPager mPager = null;     private PagerAdapter mPageAdapter = null;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         final View reVal = inflater.inflate(R.layout.activity_main, container, false);         mPager = (ViewPager) reVal.findViewById(R.id.pagerMainContent);         mPageAdapter = new ScreenSlidePagerAdapter(getActivity().getFragmentManager());         mPager.setAdapter(mPageAdapter);         return reVal;     }      private MainGridViewFragment mainGridFragment;     private AlphabetGridViewFragment alphabetGridFragment;      private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {         public ScreenSlidePagerAdapter(FragmentManager fm) {             super(fm);         }          @Override         public android.app.Fragment getItem(int position) {             switch (position) {                 case 1:                     mainGridFragment = new MainGridViewFragment();                     return mainGridFragment;                 case 0:                     alphabetGridFragment = new AlphabetGridViewFragment();                     return alphabetGridFragment;                 default:                     return null;             }         }          @Override         public int getCount() {             return 2;         }     } } 

EDIT 2 The Activity After Screen Rotation

Obviously MainActivity and MainActivityFragment are loaded, and the proof is The ActionBar, notice also that ViewPager is loaded too, because you still can navigate between pages (the blue light means that the pager reached to last page) but you can't see its content.

like image 687
Heysem Katibi Avatar asked Aug 06 '13 09:08

Heysem Katibi


People also ask

Is ViewPager deprecated?

This function is deprecated. Set a drawable that will be used to fill the margin between pages. Set a drawable that will be used to fill the margin between pages. reverseDrawingOrder: Boolean, transformer: ViewPager.

How the ViewPager is implemented?

Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen. Today we're implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using Fragments too, but we'll discuss that in a later tutorial.

Can we add activity in ViewPager?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .


2 Answers

It looks like your main activity is using the getSupportFragmentManager() while your fragment is using getFragmentManager().

Not sure if this is worthy of an answer post but my rating is too low to reply any other way. :)

Edit: I believe you may also need to extend a FragmentActivity with the support library.

See: https://stackoverflow.com/a/10609839/2640693

Edit 2:

public class MainActivityFragment extends FragmentActivity {     private ViewPager mPager = null;     private PagerAdapter mPageAdapter = null;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         final View reVal = inflater.inflate(R.layout.activity_main, container, false);         mPager = (ViewPager) reVal.findViewById(R.id.pagerMainContent);         mPageAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());         mPager.setAdapter(mPageAdapter);         return reVal;     }      private MainGridViewFragment mainGridFragment;     private AlphabetGridViewFragment alphabetGridFragment;      private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {         public ScreenSlidePagerAdapter(FragmentManager fm) {             super(fm);         }          @Override         public Fragment getItem(int position) {             switch (position) {                 case 1:                     mainGridFragment = new MainGridViewFragment();                     return (Fragment)mainGridFragment;                 case 0:                     alphabetGridFragment = new AlphabetGridViewFragment();                     return (Fragment)alphabetGridFragment;                 default:                     return null;             }         }          @Override         public int getCount() {             return 2;         }     } } 
like image 99
Jon Avatar answered Sep 28 '22 20:09

Jon


I tried many solution but get success in any one. At last I fixed as below:

  • Use getActivity().getSupportFragmentManager() rather than getChildFragmentManager()

  • Override getItemId(int position) method in your ViewPagerAdapter class like below:

    @Override public long getItemId(int position) {     return System.currentTimeMillis(); } 

For me it's working like charm, please also comment here your view.

like image 24
Kalpesh Avatar answered Sep 28 '22 20:09

Kalpesh