The main difference between the add () and replace () method from the fragment

How Fragment replace and add methods work differently, and is there a real-life scenario where we need these methods for specific purposes.

+7
android android-fragments
source share
2 answers

An important difference is the following:

replace removes the existing fragment and adds a new fragment.

but add saves existing fragments and adds a new fragment, which means that the existing fragment will be active, and they will not be in the paused state, so when you click the Back button, onCreateView() not called for the existing fragment (the fragment that was there before adding a new fragment).

For more information, just visit this conversation.

+15
source share
 fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag) 

Description It replaces the existing fragment added to the container. This is essentially the same as calling remove(Fragment) for all the fragments currently added that were added with the same container, and then add(int, Fragment, String) with the same arguments given here.

 fragmentTransaction.add(int containerViewId, Fragment fragment, String tag) 

Description It adds a fragment to the state of activity. This fragment may also have its own form (if Fragment.onCreateView returns nonzero) in the activity container view.

Please visit the official Android developer link below for more information on snippets ... http://developer.android.com/guide/components/fragments.html

+2
source share

All Articles