8

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 imply that my activity's onCreate() method is finishing after onStart()? That can't be the case ... Can it? When in my activity's lifecycle is my fragment's onActivityCreated() called? Furthermore, if I have multiple fragments, how can I control the order of the fragments' onActivityCreated() calls?

In my activity:

@Override
protected void onStart() {
    super.onStart();
    methodA(); // this is called ...
}

In my fragment:

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    methodB(); // ... before this
}
5
  • 1
    Have you checked the Activity documentation?. It's pretty clear about onStart : Called when the activity is becoming visible to the user.. Commented Jul 5, 2015 at 16:57
  • I'm not asking about when onStart it's called, I'm asking about when onActivityCreated is called.
    – GameKyuubi
    Commented Jul 5, 2015 at 17:00
  • In that case you can still see the Creating a fragment documentation and Fragment documentation Commented Jul 5, 2015 at 17:04
  • It says that onActivityCreated() is called when the activity's onCreate() method has returned, which means that onCreate has NOT returned yet when onStart is called. This means that I have to rely on onActivityCreated in my fragments, but my fragments communicate with each other so I need to know the order in which they are called.
    – GameKyuubi
    Commented Jul 5, 2015 at 17:10
  • Notice that it is its own Activity.onCreate(), not the Main Activity's, just in case. Now, if you need to know the order, when you add the fragments give them an index, position, or whatever you want to call it. Then if you need to pass information between them I think it would be a good idea to use SharedPrerences. Commented Jul 5, 2015 at 17:19

1 Answer 1

15

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.