For a solution that works in Android M, as well as in older versions, use reflection as follows:
int currentItem = 5; // Set initial position first... Field field = ViewPager.class.getDeclaredField("mRestoredCurItem"); field.setAccessible(true); field.set(mPager, currentItem); // ...and then set adapter mPager.setAdapter(adapter);
Using reflection is safe because you control the implementation of ViewPager (it is part of your application).
If you are using Proguard, you need to include the following in its configuration:
-keepclassmembers class android.support.v4.view.ViewPager { private int mRestoredCurItem; }
or the mRestoredCurItem field will be renamed to Proguard.
source share