I am trying to learn more about fragments. I read that it doesn't have an implicit stack that will handle onBackPress() like activities do so we need to add it to an explicit stack.
My question is can we call add() function more then once or we need to call replace() once the first fragment is added.
Here i am calling add() on FragmentA then calling add() on FragmentB. The fragment backstack count is being increased after i call add but the screen still showing the first fragment i.e, 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()
}
}
also why is the backstack count is still 0 once first fragment is added.
Here is the main 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>