Where to restore fragment state that is inside ViewPager

懵懂的女人 提交于 2019-12-03 03:27:51

When the ViewPager saves its state, it calls saveState() on its adapter. Similarly, when the ViewPager goes to restore its state, it calls restoreState on the adapter.

You could implement these methods, but that's already been done. Simply extend FragmentStatePagerAdapter instead of FragmentPagerAdapter and you get those method implementations for free.

If you extend FragmentStatePagerAdapter then implement onSaveInstanceState() and restore the state in onCreateView() for your Fragment classes, all should work as you expect.

So I found where the problem was. Just posting the answer here. So when the screen is rotated, although I am updating the data source, I have not reattached the listview with the updated adapter. So I just did that and it works.

if(savedInstanceState!=null) {
        Log.v("---------------->","restored!");
        nowDataList = savedInstanceState.getParcelableArrayList("savedNowDataList");
        nowForecastAdapter = new NowForecastAdapter(this, nowDataList); 
        lvNowList.setAdapter(nowForecastAdapter);           
        nowForecastAdapter.notifyDataSetChanged();
    }

Just posted the answer if anyone made the same mistake and was wondering what went wrong.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!