问题
I am using fragment transactions to switch between components on a button press. To make the overall experience better I added custom animations to animate the old fragment out to the left, and the new fragment in from the right.
The code to start this transaction looks like this:
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left)
.replace(R.id.fragment_container, contentModel.contentFragment, CONTENT_FRAGMENT_TAG)
.commit()
The animations I use look like this for enter_from_right.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="450">
<translate
android:fromXDelta="100%"
android:toXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"/>
</set>
and exit_to_left.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"/>
</set>
EDIT The fragments I am replacing look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:paddingTop=“20”
android:layout_margin=“10”
android:orientation="vertical">
<TextView
android:lineSpacingExtra=“7”
android:id="@+id/questionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom=“10”
tools:text=“Question title text“/>
<LinearLayout
android:id="@+id/textInputContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Dynamically filled with TextInputLayout and TextInputEditText elements containing answers -->
</LinearLayout>
<FrameLayout
android:layout_marginTop=“10”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/nextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text=“Next question” />
</FrameLayout>
</LinearLayout>
However, if I pause the app (press home button) when the fragment transaction is busy animating (so between button click and +- 1 second), and then I go back to the view, the following weird behaviour happens:
The fragment I am replacing (so the one that should be removed after the animation) is still visible on the screen, but I cannot interact with it, and it does not show up anywhere in the layout inspector of Android Studio.
It also does not go behind the normal content, it goes in front of the normal content (but clicks pass through it).
The only thing that seems to work, but I dont want to use that, is to add addToBackStack to the transaction, since I'm not using the transaction back stack and then I need to add ugly code to clear up the back stack.
I wonder if anyone else encountered this issue and got a good solution for it.
回答1:
Try calling executePendingTransactions()
after calling commit()
.
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left)
.replace(R.id.fragment_container, contentModel.contentFragment, CONTENT_FRAGMENT_TAG)
.commit()
.executePendingTransactions()
来源:https://stackoverflow.com/questions/53104873/ghost-behavior-when-pausing-activity-while-fragment-replace-transaction-with-cus