Difference between add(), replace(), and addToBackStack()

后端 未结 9 1586
终归单人心
终归单人心 2020-11-22 05:40

What is the main difference between calling these methods:

fragmentTransaction.addToBackStack(name);
fragmentTransaction.replace(containerViewId, fragment, t         


        
9条回答
  •  被撕碎了的回忆
    2020-11-22 06:22

    Basic difference between add() and replace() can be described as:

    • add() is used for simply adding a fragment to some root element.
    • replace() behaves similarly but at first it removes previous fragments and then adds next fragment.

    We can see the exact difference when we use addToBackStack() together with add() or replace().

    When we press back button after in case of add()... onCreateView is never called, but in case of replace(), when we press back button ... oncreateView is called every time.

提交回复
热议问题