问题
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.
回答1:
The possible issue may be the code:
if (mFragmentFeedBack.isVisible())
I don't recommend using this method for checking visibility. According to documentation @ Fragment isVisible(), it says
...This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden.
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:
- Create a boolean flag, and maintain the flag between the 2 fragments. If this is simple to do, this is best.
- Check if a button or view is clickable, don't know which one. This is more solid than checking for isVisible().
- I think the code design is a bit more complicated than it should be. For now, this is my suggestion. When user hits "New Button", just call the replace() method, don't use the hide/show methods. When user hits the SEND button in the feedback fragment, either call popBackStack() or replace() methods. How about that?
来源:https://stackoverflow.com/questions/30731107/fragments-transaction-replace-on-api-21-is-staying-behind