What's the difference between detaching a Fragment and removing it?

前端 未结 2 1633
傲寒
傲寒 2020-12-04 05:43

In the Android docs for a FragmentTransaction I noticed two very similar methods: detach and remove. The descriptions over there don\'t seem to pro

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 06:00

    The detach method removes the fragment from the UI, but its state is maintained by the Fragment Manager. This means you can reuse this fragment by calling the attach method, with a modified ViewHierarchy

    Remove means the fragment instance cannot be re-attached. You will have to add it again to the fragment transaction.

    Source Comment

    You'll notice that when a Fragment is detached, its onPause, onStop and onDestroyView methods are called only (in that order). On the other hand, when a Fragment is removed, its onPause, onStop, onDestroyView, onDestroy and onDetach methods are called (in that order). Similarly, when attaching, the Fragment's onCreateView, onStart and onResume methods are called only; and when adding, the Fragment's onAttach, onCreate, onCreateView, onStart and onResume methods are called (in that order). – Adil Hussain

提交回复
热议问题