I am developing an app that uses fragments, last week my test device took lolipop update. When I test my app on a lolipop device, I saw that Fragment Transaction's replace method didn't work properly.
It work with confusingly in Lolipop version although everything fine on Kitkat version.
In order to explain my situation, I've added some images.
--First Screen----------------------------KitKat-------------------------------------Lollipop-------------
As you can see, when i use kitkat
, everything fine but as soon as i use lolipop
fragment transaction replace is working confusingly.
Here is my button code;
mButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FeedbackFragment mFragmentFeedBack = new FeedbackFragment();
android.app.FragmentManager fm = getFragmentManager();
fm.executePendingTransactions();
android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (mFragmentFeedBack.isVisible()) {
fragmentTransaction.hide(mFragmentFeedBack);
} else {
if (!mFragmentFeedBack.isAdded()) {
fragmentTransaction.replace(R.id.containerfragment, mFragmentFeedBack);
}
fragmentTransaction.show(mFragmentFeedBack);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
here is my xml;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerfragment">
</FrameLayout>
EDIT: Kitkat version is running on a tablet, but I tried my app on the phone (Kitkat version) result is the same. No changes.
Thanks.
The possible issue may be the code:
I don't recommend using this method for checking visibility. According to documentation @ Fragment isVisible(), it says
This part of the sentence is not very clear. I suspect KitKat says it is NOT visible but Lollipop says it is, and I agree with Lollipop implementation. KitKat says the Fragment is added (yes), the view is attached (yes), hidden (not really!). This is actually a GUI issue with other GUI libraries, believe it or not!
Possible solutions, for now: