I have the following situation and couldn't find a solution so far: In Activity A there is a ViewPager with a FragmentStatePagerAdapter. From Activity A you call Activity B. There you can change the some global data, what also affects the number of pages in the ViewPager in Activity A. So as soon I use the back button, to return to Activity A, I get the following Exception:
E/AndroidRuntime(1756): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 121, found: 151 Pager id: com.example.blablabla:id/main_view_pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.example.blablabla.ActivityMain$MainViewPagerAdapter
E/AndroidRuntime(1756): at android.support.v4.view.ViewPager.populate(ViewPager.java:962)
E/AndroidRuntime(1756): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
...
I tried calling notifyDataSetChanged(); on the adapter at different places (onCreate, onRestart, onResume, onActivityResult) but nothing worked.
Another thing I tried was setting the adapter to null everytime Activity A is paused and then recreating it, when the activity is resumed. This avoids the crash, but 1. doesn't feel right and 2. the pages disappear shortly before the new activity becomes visible, so it does not look good.
So, how to handle this situation correctly?