【问题标题】:Android Fragments overlapping when popping back弹出时Android Fragments重叠
【发布时间】:2018-11-20 10:31:38
【问题描述】:

我有一个 Fragment 容器,它可以显示当前的 Fragment。当按下后退按钮时,我想删除最上面的片段,但是当它是第一个片段时,我想在后台移动应用程序。 (否则,我的主要片段也会被删除。)

所以我的 (Kotlin) 代码如下所示:

override fun onBackPressed() {
    ...
    when (supportFragmentManager.backStackEntryCount) {
        1    -> {
            moveTaskToBack(true)
        }
        else -> {
           super.onBackPressed()
           //supportFragmentManager.popBackStackImmediate()
        }
    }

这工作正常,但有一个例外。当我启动应用程序并显示第二个片段时,我按两次后退按钮(快速)。这样,顶部的 Fragment 开始被移除,但第二次后压紧接着开始(顶部的 Fragment 没有完全移除)。 该应用程序进入后台(按预期),但是当我再次将应用程序带入前台时,顶部 Fragment 仍然存在(有时已经部分删除)但它不再反应。如果我替换容器中的另一个片段,它会出现在卡住的片段后面。 我也尝试使用 supportFragmentManager.popBackStackImmediate() 代替,但也没有用。

所以我有两个问题:

  • 之后如何继续清理(当应用再次可见时)

  • 或者如果这不是解决方案,我如何检查 FragmentManager 中是否仍有 Fragment 事务正在进行(在通过第二次后按将应用程序置于后台之前)

---- 编辑 1 ----

我注意到只有在替换片段时设置事务动画时才会发生这种情况。因此,如果我在添加片段时删除该行(而不是在后按时),它似乎可以工作:

  ft.setCustomAnimations(R.anim.enter_from_bottom, R.anim.exit_to_top,
   R.anim.enter_from_top, R.anim.exit_to_bottom)

---- 编辑 2 ----

在这里使用此代码时,它也可以工作(只是它也删除了第一个(底部)片段。但显然原生 Android 方法似乎避免了这个问题。

super.onBackPressed() // Remove bottom fragement
super.onBackPressed() // No more fragments -> move App into background

---- 这就是“创建”片段的方式----

val ft = supportFragmentManager.beginTransaction()
ft.setCustomAnimations(R.anim.enter_from_bottom, R.anim.exit_to_top, 
    R.anim.enter_from_top, R.anim.exit_to_bottom)
ft.replace(R.id.fragmentContainer, fragment, fragment::class.java.simpleName)
    .addToBackStack(fragment::class.java.simpleName)
    .commitAllowingStateLoss()

这是重叠片段的外观截图:

【问题讨论】:

  • 如何打开片段?你能提供你的代码,它负责打开你的片段吗?
  • 好的,我添加了showFragment()的重要部分。简单地说,这只是替换了下面的 Fragment。
  • 我不太确定,但可能是给定标签 (fragment::class.java.simpleName) 的问题。您对replaceaddToBackStack 方法使用相同的标签。也许您可以检查这是否是调用此方法的正确方法。
  • 不,那行得通。如问题中所述,如果我做得足够慢,一切都会很好。片段被弹出,显示了底层的片段。只是“移至背景”似乎与弹出动画或类似的东西混淆了。这不是不识别碎片左右的问题。

标签: android android-fragments kotlin


【解决方案1】:

试试这个....

在你的 xml 中使用 FrameLayout

android:background="#ffffff"

并在主视图组件上添加所有片段 .xml,例如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout> 

【讨论】:

  • 感谢您的建议,但我的问题不是片段透明度。真正的问题是有一个片段在过渡过程中“卡住”了。看起来过渡只完成了一半,然后就被遗忘了。下面的片段被那一半消失的片段挡住了。所以这不是可见性问题,而是片段堆栈/过渡的问题。
猜你喜欢
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多