ViewPager exit on orientation


#1

Hello,
my ViewPager exits on screen orientation. can’t seem to figure out why. can someone help?

package com.bignerdranch.android.criminalintent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.List;
import java.util.UUID;

public class CrimePagerActivity extends AppCompatActivity {
public static final String EXTRA_CRIME_ID =
        "com.bignerdranch.android.criminalintent.crime_id";

private ViewPager mViewPager;
private Button mJumpToFirst;
private Button mJumpToLast;
private List<Crime> mCrimes;

public static Intent newIntent(Context packageContext, UUID crimeId) {
    Intent intent = new Intent(packageContext, CrimePagerActivity.class);
    intent.putExtra(EXTRA_CRIME_ID, crimeId);
    return intent;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crime_pager);

    UUID crimeId = (UUID) getIntent()
            .getSerializableExtra(EXTRA_CRIME_ID);

    mViewPager = (ViewPager) findViewById(R.id.crime_view_pager);
    mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.view_pager_margin));

    mCrimes = CrimeLab.get(this).getCrimes();
    FragmentManager fragmentManager = getSupportFragmentManager();
    mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
        @Override
        public Fragment getItem(int position) {
            // Log.d("CrimePagerActivity", "getItem()  > " + position);

            Crime crime = mCrimes.get(position);
            return CrimeFragment.newInstance(crime.getId());
        }

        @Override
        public int getCount() {
            // Log.d("CrimePagerActivity", "getCount()");
            return mCrimes.size();
        }
    });

    mViewPager.setOffscreenPageLimit(4);
    for(int i = 0; i < mCrimes.size(); i++) {
        if (mCrimes.get(i).getId().equals(crimeId)) {
            mViewPager.setCurrentItem(i);
            break;
        }
    }

    mJumpToFirst = (Button) findViewById(R.id.jump_to_first_button);
    mJumpToFirst.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mViewPager.setCurrentItem(0);
        }
    });

    mJumpToLast = (Button) findViewById(R.id.jump_to_last_button);
    mJumpToLast.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mViewPager.setCurrentItem(mCrimes.size() - 1);
        }
    });

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
            mJumpToFirst.setEnabled(mViewPager.getCurrentItem() != 0);
            mJumpToLast.setEnabled(mViewPager.getCurrentItem() != mCrimes.size() - 1);
        }

        @Override
        public void onPageSelected(int i) {
            // Nothing
        }

        @Override
        public void onPageScrollStateChanged(int i) {
            // Nothing
        }
    });

}
}