Activity onStart() being called before Fragment's onActivityCreated()

前端 未结 1 807
北海茫月
北海茫月 2021-02-15 17:46

I\'m having an issue where my fragment\'s onActivityCreated() method is being called after my activity\'s onStart() method is called. This seems to im

相关标签:
1条回答
  • 2021-02-15 18:24

    onActivityCreated() method is being called after my activity's onStart() method is called

    Remember that onActivityCreated() method just a callback for the fragment from activity.

    This seems to imply that my activity's onCreate() method is finishing after onStart()? That can't be the case ... can it?

    Wrong! Activity and fragment is separate, So onCreated() method in Activity and onActivityCreated() method in fragment could not be the same. As above, in Fragment it's just a callback mapping with activity state.

    Let's have a look at this picture to have a better understanding. enter image description here

    In Official document from Google: Activity onStart()

    Called just before the activity becomes visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

    Fragment callback: onActivityCreated()

    Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

    The last one:

    Furthermore, if I have multiple fragments, how can I control the order of the fragments' onActivityCreated() calls?

    It's depend on which way you use to add your fragments to activity. Basically the order will be the order of added fragments.

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