Home > Software engineering >  call add() twice with different fragment
call add() twice with different fragment

Time:04-11

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.

enter image description here

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>

CodePudding user response:

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

You are using a LinearLayout as a container of the fragments. When your fragments are getting added, they are going out of the screen as they are getting added vertically below the first fragment. That's why you are always seeing your first fragment on the screen.

You should use a FrameLayout as a container, so you can see your last transacted fragment on the screen.

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

commit() method is asynchronous, so when you update fragment_stack_count text, the backstack count is still not increased as fragment transactions are not yet completed, and the fragment is still not get added to the backstack.

An excerpt from the Android doc

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.

You can read more about it on documentation of fragment transactions

  • Related