'Why `supportFragmentManager.backStackEntryCount` is empty after `add()` fragment to stack

In Android app (Kotlin) I have Activity with container what have fragment A in it. On user interaction, I am adding fragment B to container by:

supportFragmentManager.commit {
        add(container, fragmentB, tag)
    }

Why when I go back in stack by hitting hardware back button (calls onBackPressed() in activity) the supportFragmentManager.backStackEntryCount is zero?

Do I need to also call addToBackStack() in transaction to be able to intercept back behavior and decide if I want to go back or not?

Will add() + addToBackStack() in transaction and supportFragmentManager.popBackStack() in onBackPressed() method cause re-init of the fragment A?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source