Fragment without activity

后端 未结 7 1765
夕颜
夕颜 2020-12-29 06:14

I have been asked an interview question: Can a fragment exist without activity? I searched for answers but didn\'t get a proper answer and explanation. Can

7条回答
  •  生来不讨喜
    2020-12-29 06:39

    As soon as you create an instance of the Fragment class, it exists, but in order for it to appear on the UI, you must attach that fragment to an activity because a fragment's lifecycle runs parallel to an activity's lifecycle. Without any call to Activity's onCreate(), there will be no call for onAttach(), onCreate(), onCreateView() and onActivityCreated() of fragment and so it can't be started.

提交回复
热议问题