I hav a problem. I have a fragment, which contains viewpager
inside. For viewpager
i use FragmentPagerAdapter extension. There are recycler views
inside of each pager fragment
.
The question is that i can't restore scroll position after screen rotation inside recycler views
of viewpager
. I thought that problem is in recyclerview
, but soon i found out that after screen rotation fragments inside viewpager
are being recreating, so onCreate()
and onDestroy()
are called, even though they shouldn't. I also tried use onSaveInstanceState(bundle)
and found out that inside onCreateView()
this bundle is always null. So what should i do?
This topic didn't help me.
Asked
Active
Viewed 806 times
5

Community
- 1
- 1

Geron Thunder
- 81
- 1
- 6
-
Could you try using mViewPager.setOffscreenPageLimit(limit); – dex Nov 02 '15 at 16:48
-
@dex, i tried. it did not help me. actually, it shouldn't because viewpager saves fragments' state when i swipe it, but when i rotate screen, position is got lost. sorry for my english – Geron Thunder Nov 02 '15 at 17:15
3 Answers
3
Well, i have found an answer. The problem is in me). There is hierarchy : MainActivity with frame layout for fragment - PagerFragment which contains viewpager - fagments inside viewpager. The problem is that after screen rotation inside of activity i was allways replacing PagerFragment, such as:
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragment = new PagerFragment();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
but i should do this:
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment == null) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragment = new PagerFragment();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
It was completely my fault and i am ashamed. I am sorry.

Geron Thunder
- 81
- 1
- 6
0
You can override function in your PagerAdapter with null state.
override fun restoreState(state: Parcelable?, loader: ClassLoader?) {
super.restoreState(null, loader)
}
0
Those two overrides should solve your problem
here you save your values
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}
and here you can restore it
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
}

Kmelliti
- 101
- 7