2

I have been tring since this morning to handle screen rotation with the viewpager, i tried a lot of things i read almost every post here about this and i was unable to find my issue.

Issues

  1. When i change the oriention it deletes 3 or 4 pages, i have to swipe right 3 or 4 times in order to see other pages. Changing from to portrait works almost everytime, no pages get deleted, but if i go from portrait to landscape the pages disappear

I read in one of the post that this is due the fragment being distroyed when orientation changes and that we need to use setRetainInstance(true); inside on create to fix this but it's not working, am i missing something here?

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        if (position <= (arraySize - 1)) {
            HashMap<String, String> data = computeQuestion(questoes[randomize.get(position)]);
            return PlaceholderFragment.newInstance(position + 1, data, tables[randomize.get(position)]);
        } else {
            return FragmentResultados.newInstance(position + 1);
        }
    }

    @Override
    public int getCount() {
        return arraySize + 1; // number of pages + ending page
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
            default:
                return String.valueOf(position);
        }
    }
    }

Fragment

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

EDIT

Fragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_exame, container, false);

        mViewPager = (ViewPager) getActivity().findViewById(R.id.container);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        return rootView;
    }

Result: IllegalStateException: FragmentManager is already executing transactions

Activity

 private static ViewPager mViewPager;
 private static FragmentStatePagerAdapter mSectionsPagerAdapter;  

      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exame2);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    }

Manifest

 <activity
        android:name=".Exame"
        android:label="@string/title_activity_exame"
        android:theme="@style/AppTheme"
        android:configChanges="keyboardHidden|orientation|screenSize"/>

activity_xml

<android.support.design.widget.CoordinatorLayout      xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="pt.condutorresponsavel.android.testescodigo.Exame">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<ImageView
    android:id="@+id/arrowLeft"
    android:background="@color/colorPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_first_page_white_24dp"
    android:paddingLeft="10dp"
    android:paddingTop="3dp"
    android:paddingRight="10dp"
    android:paddingBottom="3dp"
    android:onClick="toFirst"
   />

<ImageView
    android:background="@color/colorPrimary"
    android:onClick="toLast"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_last_page_white_24dp"
    android:paddingLeft="10dp"
    android:paddingTop="3dp"
    android:paddingRight="10dp"
    android:paddingBottom="3dp"
    android:layout_alignParentRight="true"
    android:id="@+id/arrowRight" />

<TextView
    android:id="@+id/timeleft"
    android:textColor="#ffffff"
    android:background="@color/colorPrimary"
    android:paddingRight="10dp"
    android:paddingLeft="5dp"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="20:01"
    android:layout_alignBottom="@+id/arrowRight"
    android:layout_toLeftOf="@+id/arrowRight"
    android:layout_toStartOf="@+id/arrowRight"
    android:layout_alignParentTop="true" />

i uploaded a sample o the code to github

FIXED

private void InitializeUI(){
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_exame2);
    InitializeUI();
}
Tiago Oliveira
  • 1,582
  • 1
  • 16
  • 31
  • Why you call getActivity().findViewById() in your **fragment** and not use rootView.findViewById() ? – Amir Aug 18 '16 at 17:41
  • When you rotate screen Whole Activity destroy so you should store current index of ViewPager in saveInstance and retrieve it in onCreate. – Amir Aug 18 '16 at 17:42
  • I am using getActivity because if i use rootView returns null, the viewpager is inside the activity_layout,not inside fragment_layout, this is one of the things that i don't understand every exemple that i see they use rootView, the activity is not destroyed onCreate i'ts only called once, i reset the views in InitializeUI(), i have a timer that displays the time limit inside onCreate, when orientation changes the time remains the same. i updated the github file, it's just a sample i have almost 2k lines must of the stuff is not relevant to this but i can post the full code – Tiago Oliveira Aug 18 '16 at 19:21

4 Answers4

2

I was missing android:configChanges="orientation|screenSize" in the manifest file. E.g.:

<activity
    android:name=".MyActivity"
    android:configChanges="orientation|screenSize" />
sp00ky
  • 151
  • 9
1

Do you use android support library? This may be an issue. Also look at similar problem here: ViewPager fragments disappear when change screen rotation

Community
  • 1
  • 1
zkvarz
  • 611
  • 1
  • 8
  • 18
  • 1
    I am losing all the hope on this, i tried all awnsers on that post none of them worked, yes i am using the support library – Tiago Oliveira Aug 18 '16 at 00:06
  • Here is a similar problem. getChildFragmentManager should be used, it seems http://stackoverflow.com/questions/25216464/viewpage-fragment-disappear-when-reload-again – zkvarz Aug 18 '16 at 00:44
  • thanks alot for you help, but this setAdapter(adapter); is allways trowing an IllegalStateException says FragmentManager is already executing transactions – Tiago Oliveira Aug 18 '16 at 02:01
  • I don't know your code right now, I think you need to update your question :) And try to follow instructions on my links exactly, there is no reason that it would't work. – zkvarz Aug 18 '16 at 07:22
  • 1
    @tiago-oliveira Also, try to add: android:configChanges="screenSize|orientation" In AndroidManifest.xml – zkvarz Aug 18 '16 at 13:54
  • i updated my question, i think im doing everything that they say, i have the configchanges as well – Tiago Oliveira Aug 18 '16 at 16:21
  • 1
    i was finally able to fix this issue :D, so i removed the setAdapter form the fragment and i place it in InitializeUI() that worked like a charm – Tiago Oliveira Aug 18 '16 at 21:13
1

I know it is really late. For others having this issue, I had the same problem: I fixed it using this .

Lane
  • 43
  • 5
Eren
  • 11
  • 2
1

If your fragment pages dissapear then it will work:-

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

@Override
public long getItemId(int position) {
    return System.currentTimeMillis();
}
Amit raj
  • 390
  • 3
  • 8