使用不同的片段调用 add() 两次

call add() twice with different fragment

我正在努力学习更多关于片段的知识。我读到它没有像活动那样处理 onBackPress() 的隐式堆栈,因此我们需要将它添加到显式堆栈。

我的问题是我们可以多次调用 add() 函数还是我们需要在添加第一个片段后调用 replace()。

这里我在 FragmentA 上调用 add() 然后在 上调用 add()片段B。在我调用 add 但屏幕仍然显示第一个片段后,片段堆栈计数正在增加,即 FragmentA.

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fragment_stack_count.text = supportFragmentManager.backStackEntryCount.toString()



        add_a.setOnClickListener { it
            addFragmentA()
        }

        add_b.setOnClickListener {
            addFragmentB()
        }
    }

    fun addFragmentA() {
        val fragA = FragmentA()
        supportFragmentManager.beginTransaction().add(R.id.container, fragA).addToBackStack("A").commit()
        fragment_stack_count.text = supportFragmentManager.backStackEntryCount.toString()

    }

    fun addFragmentB() {
        val fragB = FragmentB()
        supportFragmentManager.beginTransaction().add(R.id.container, fragB).addToBackStack("B").commit()
        fragment_stack_count.text = supportFragmentManager.backStackEntryCount.toString()
    }

    
}

另外,为什么在添加第一个片段后返回堆栈计数仍然为 0。

这里是主要内容activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D8EFFA"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="12dp"
        android:background="@color/white"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.8"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </LinearLayout>


    <TextView
        android:id="@+id/fragment_stack_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/add_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD A"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/add_b"
        app:layout_constraintTop_toBottomOf="@+id/container" />

    <Button
        android:id="@+id/add_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD B"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/add_a"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/container" />
</androidx.constraintlayout.widget.ConstraintLayout>

after I call add but the screen still showing the first fragment

您正在使用 LinearLayout 作为片段的容器。当您的片段被添加时,它们会离开屏幕,因为它们被垂直添加到第一个片段的下方。这就是为什么您总是在屏幕上看到您的第一个片段。

您应该使用 FrameLayout 作为容器,这样您就可以在屏幕上看到您最后的交易片段。

why is the backstack count is still 0 once first fragment is added

commit() 方法是异步的,因此当您更新 fragment_stack_count 文本时,由于片段事务尚未完成,因此后台堆栈计数仍未增加,片段仍未添加到后栈。

摘自 Android 文档

Calling commit() doesn't perform the transaction immediately. Rather, the transaction is scheduled to run on the main UI thread as soon as it is able to do so.

您可以在 fragment transactions

的文档中阅读更多相关信息