Fragment Transaction Replaces API-21 Remains

I am developing an application that uses fragments, last week my test device accepted the lolipop update. When I test my application on a lolipop device, I saw that the Fragment Transaction replacement method is not working properly.

It works with confusion in the Lolipop version, although everything is fine on the Kitkat version.

To explain my situation, I added some images.

- First screen ---------------------------- KitKat -------------- ---- ------------------- lollipop -------------

enter image description hereenter image description hereenter image description here

As you can see, when I use kitkat , everything is fine, but as soon as I use lolipop fragment transaction lolipop , it works dimly.

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 works on the tablet, but I tried my application on the phone (Kitkat version), same thing. Without changes.

Thanks.

+5
source share
1 answer

A possible problem might be the code:

 if (mFragmentFeedBack.isVisible()) 

I do not recommend using this method to check visibility. According to the documentation @ Fragment isVisible () , it says

... This means that it: (1) was added, (2) has its own view attached to the window and (3) is not hidden.

This part of the sentence is not very clear. I suspect KitKat says this is NOT visible, but Lollipop says that it is, and I agree with the implementation of Lollipop. KitKat says that the fragment is added (yes), the view is attached (yes), it is hidden (not really!). This is actually a GUI problem with other GUI libraries, believe it or not!

Possible solutions so far:

  • Create a boolean flag and save the flag between two fragments. If this is easy to do, this is best.
  • Check if the button or view is available, I don’t know which one. This is harder than checking for isVisible ().
  • I think the code design is a bit more complicated than it should be. This is my suggestion at the moment. When the user clicks the "New Button", just call the replace () method, do not use the hide / show methods. When the user clicks the SEND button in the feedback fragment, the popBackStack () or replace () method is called. How about this?
0
source

All Articles