What is the main difference between calling these methods:
fragmentTransaction.addToBackStack(name);
fragmentTransaction.replace(containerViewId, fragment, t
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.