I have a page viewer and something really weird is happening on API 21 (It works on API 17).
On the first page of the viewer there’s a link that loads the second page and replaces a fragment in It. The first time I click the link and load the second page It works fine, but if I click on the link after having loaded a third page, the fragment replace doesn’t work and I end up with the second screen without the fragment in it.
Here is a simplified version of code that sets the second page:
ScreenSlidePagerAdapter adapter = (ScreenSlidePagerAdapter) sPager.getAdapter();
// Set second page in the viewer
if (adapter.getCount() != 2) {
adapter.setCount(2);
adapter.notifyDataSetChanged();
}
SecondPageFragment frag2 = new SecondPageFragment();
sFragmentManager.beginTransaction().replace(R.id.container_page2, frag2, “Frag2TAG”).commit();
sPager.setCurrentItem(1);
Why does replace work differently on API 21 if compared with API 17? Anyone had a similar problem before? This seems to be related but doesn't have a solution: fragments-transaction-replace-on-api-21-is-staying-behind
EDIT: the code for the SecondPageFragment is just a Fragment with this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved_instance_state) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_page2, container, false);
return rootView;
}
And this is the layout fragment_page2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/container_page2">