Basic difference between add() and replace() metho

2019-01-18 13:30发布

This question already has an answer here:

How do Fragment's replace and add methods work differently, and is there any real life scenario where we would need these methods for specific purposes.

2条回答
混吃等死
2楼-- · 2019-01-18 13:48

The important difference is:

replace removes the existing fragment and adds a new fragment..

but add retains the existing fragments and adds a new fragment that means existing fragment will be active and they wont be in 'paused' state hence when a back button is pressed onCreateView() is not called for the existing fragment(the fragment which was there before new fragment was added).

For more information just visit this conversation.

查看更多
劫难
3楼-- · 2019-01-18 13:57
fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag)

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

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

Description - It add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

Please visit below official android developer link for more details about fragments... http://developer.android.com/guide/components/fragments.html

查看更多
登录 后发表回答