Android Fragment onCreateView vs. onActivityCreated

后端 未结 2 1033
情深已故
情深已故 2020-11-30 18:52

I know that a fragment\'s view hierarchy has to be inflated in onCreateView, but what other functionality can be in onCreateView vs. what should wait for onActivityCreated?

相关标签:
2条回答
  • 2020-11-30 19:41

    If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so.

    Also accessing the view hierarchy of the parent activity must be done in the onActivityCreated, not sooner.

    0 讨论(0)
  • 2020-11-30 19:45

    onActivityCreated() is deprecated in fragment 1.3.0-alpha02 and there is a recommendation to use onViewCreated() instead. View is already created here and you can set listeners, observe LiveData from ViewModel, initialize recyclerView, etc.

    For a better understanding, you can take a look at my blog post, where I describe the Android Fragment lifecycle in 137 seconds.

    0 讨论(0)
提交回复
热议问题