I'm having problems restoring the View state inside the ViewPager. The view that I insert in the ViewPager is a CustomView extending FrameLayout.
Here is the code of my Activity.java
private ViewPager vPager; private MainPagerAdapter mAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { setContentView(R.layout.activity_layout);
MainPagerAdapter is the class from the accepted answer of this question
CustomView.java
@Override protected Parcelable onSaveInstanceState() { Log.d(TAG, "onSaveInstanceState() called"); } @Override protected void onRestoreInstanceState(Parcelable state) { Log.d(TAG, "onRestoreInstanceState() called"); }
Here are my findings:
- onSaveInstanceState () will be called, but onRestoreInstanceState () is not
- When I tried to add the View directly to the root of the Activity, it calls both functions.
- I found out that the Activity onRestoreState function is called before the ViewPager calls the
instantiateItem() adapter function. Therefore, when an action restores its state, the ViewPager does not yet have children, therefore, the saved state has no owner.
So, I realized that I need to do one of two things:
- Verify that the ViewPager instantiates the item before attempting to restore the state or
- Calling CustomView
onRestoreInstanceState() manually.
I somehow managed to make option number 2, but is there a way to make option number 1?
source share