我正在嘗試更多地了解片段。我讀到它沒有像活動一樣處理 onBackPress() 的隱式堆疊,因此我們需要將其添加到顯式堆疊中。
我的問題是我們是否可以多次呼叫 add() 函式,或者我們需要在添加第一個片段后呼叫 replace() 。
在這里,我在 FragmentA 上呼叫 add() 然后在FragmentB上呼叫 add( )。在我呼叫 add 后,片段 backstack 計數正在增加,但螢屏仍顯示第一個片段,即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()
}
}
還有為什么一旦添加第一個片段,backstack 計數仍然為 0。

這是主要活動
<?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>
uj5u.com熱心網友回復:
在我呼叫 add 但螢屏仍然顯示第一個片段之后
您正在使用 aLinearLayout作為片段的容器。當你的片段被添加時,它們會離開螢屏,因為它們被垂直添加到第一個片段的下方。這就是為什么你總是在螢屏上看到你的第一個片段。
您應該使用 aFrameLayout作為容器,這樣您就可以在螢屏上看到您最后交易的片段。
為什么添加第一個片段后回堆疊計數仍為 0
commit()方法是異步的,所以當你更新fragment_stack_count文本時,由于片段事務還沒有完成,所以回堆疊計數仍然沒有增加,并且片段仍然沒有被添加到回堆疊中。
Android 檔案的摘錄
呼叫 commit() 不會立即執行事務。相反,事務被安排在主 UI 執行緒上運行,只要它能夠這樣做。
您可以在片段交易的檔案中閱讀更多相關資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459868.html
下一篇:從超類指標向量呼叫重寫的方法
