片段替换事务正在运行且自定义动画正在暂停时暂停活动时的幻影行为 [英] Ghost behavior when pausing activity while fragment replace transaction with custom animation is running

查看:33
本文介绍了片段替换事务正在运行且自定义动画正在暂停时暂停活动时的幻影行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用片段事务在按下按钮时在组件之间进行切换.为了使整体体验更好,我添加了自定义动画,以使左侧的旧片段具有动画效果,而右侧的新片段则具有动画效果.

启动此事务的代码如下:

  supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left).replace(R.id.fragment_container,contentModel.contentFragment,CONTENT_FRAGMENT_TAG).犯罪() 

我使用的动画看起来像这样,用于enter_from_right.xml:

 < set xmlns:android ="http://schemas.android.com/apk/res/android"android:startOffset ="450"><翻译android:fromXDelta ="100%"android:toXDelta ="0"android:interpolator ="@ android:anim/decelerate_interpolator"android:duration ="500"/></set> 

和exit_to_left.xml:

 <设置xmlns:android ="http://schemas.android.com/apk/res/android"><翻译android:fromXDelta ="0"android:toXDelta =-100%"android:interpolator ="@ android:anim/decelerate_interpolator"android:duration ="500"/></set> 

EDIT 我要替换的片段如下所示:

 <?xml version ="1.0" encoding ="utf-8"?>< LinearLayoutxmlns: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">< TextViewandroid:lineSpacingExtra ="7"android:id ="@ + id/questionTitle"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:layout_marginBottom ="10"工具:文本=问题标题文本"/>< LinearLayoutandroid:id ="@ + id/textInputContainer"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:orientation ="vertical"><!-动态填充包含答案的TextInputLayout和TextInputEditText元素-></LinearLayout>< FrameLayoutandroid:layout_marginTop ="10"android:layout_width ="match_parent"android:layout_height ="0dp"android:layout_weight ="1"><按钮android:id ="@ + id/nextButton"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:layout_gravity ="bottom"android:text =下一个问题"/></FrameLayout></LinearLayout> 

但是,如果我在片段事务忙于动画时(例如在按钮单击和+-1秒之间)暂停应用程序(按主屏幕按钮),然后返回视图,则会发生以下奇怪的行为:

我要替换的片段(因此应该在动画之后删除的片段)在屏幕上仍然可见,但是我无法与之交互,并且它不会出现在Android Studio布局检查器中的任何位置./p>

它也不会落后于常规内容,而是先于常规内容(但点击会通过它).

似乎唯一可行的方法,但我不想使用它,是将addToBackStack添加到事务中,因为我没有使用事务回栈,所以我需要添加丑陋的代码来清理事务堆栈.

我想知道是否还有其他人遇到此问题并找到了一个好的解决方案.

解决方案

在调用 commit()之后尝试调用 executePendingTransactions() ./p>

  supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left).replace(R.id.fragment_container,contentModel.contentFragment,CONTENT_FRAGMENT_TAG).犯罪().executePendingTransactions() 

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.

解决方案

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()

这篇关于片段替换事务正在运行且自定义动画正在暂停时暂停活动时的幻影行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆