Pavel Poley
Pavel Poley

Reputation: 5577

When Fragment onActivityCreated called

Before Google architecture component and LiveData I have not paid attention to onActivityCreated() callback. I read about this here in SOF as well in documentation, and i still cannot understand the behavior.

From one of SOF answers:

As the name states, this is called after the Activity's onCreate() has completed.

It common practice to attach LiveData observers in onActivityCreated(), so i guess there are significant difference between onActivityCreated() and onCreateView()?

Although looking on the diagram from official Android docs seems like it onActivityCreated() is called always after onCreateView()(in terms of execution, not order) and there no diffrence?

Something confusing here.

enter image description here

UPDATE: onActivityCreated() deprecated.

Upvotes: 4

Views: 1875

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

EDIT: According to Ian Lake on Twitter (see https://twitter.com/ianhlake/status/1193964829519667202), the fact that FragmentActivity attempts to dispatch onActivityCreated in onStart is irrelevant, because no matter what happens, the FragmentManager dispatches it anyway when going from onCreate to onStart.

            case Fragment.CREATED:
                // We want to unconditionally run this anytime we do a moveToState that
                // moves the Fragment above INITIALIZING, including cases such as when
                // we move from CREATED => CREATED as part of the case fall through above.
                if (newState > Fragment.INITIALIZING) {
                    fragmentStateManager.ensureInflatedView();
                }
                if (newState > Fragment.CREATED) {
                    fragmentStateManager.createView(mContainer);
                    fragmentStateManager.activityCreated(); // <--
                    fragmentStateManager.restoreViewState();

So what I said below is actually wrong.

Apparently using onActivityCreated inside a Fragment is equivalent to using onViewCreated.

But this also means you shouldn't rely on onActivityCreated to know if your Activity was actually created, because it is called more times than when the Activity is actually created.

Fragments are confusing.



ORIGINAL ANSWER:

Is it possible that onCreateView() called but onActivityCreated() not called?

UPDATE: No, it's not possible.

ORIGINAL: Yes, in a FragmentPagerAdapter they use FragmentTransaction.detach / FragmentTransaction.attach, which causes the View to be destroyed, but the Fragment stays alive (stopped, but not destroyed).

In this case, .attach() runs onCreateView, but not onActivityCreated.

It's common practice to attach LiveData observers in onActivityCreated(), so i guess there are significant difference between onActivityCreated() and onCreateView()?

UPDATE: it doesn't matter, although onViewCreated is still clearer.

ORIGINAL: It's actually a bad practice, and should be done in onViewCreated, providing getViewLifecycleOwner() as the lifecycle owner.

Although looking on the diagram from official Android docs seems like it onActivityCreated() is called always after onCreateView() and there no diffrence?

UPDATE: Despite that the FragmentActivity only tries to dispatch it once, all Fragments always go through onActivityCreated, because that's just how the FragmentManager works.

ORIGINAL: It is not always called after onCreateView, in fact, it's more-so called "before onStart, but only once".

/**
 * Dispatch onStart() to all fragments.
 */
@Override
protected void onStart() {
    super.onStart();

    mStopped = false;

    if (!mCreated) {
        mCreated = true;
        mFragments.dispatchActivityCreated(); // <---
    }

    mFragments.noteStateNotSaved();
    mFragments.execPendingActions();

    // NOTE: HC onStart goes here.

    mFragments.dispatchStart();
}

UPDATE: but apparently this doesn't really matter to the FragmentManager, because it goes CREATED -> ACTIVITY_CREATED -> STARTED either way.

Upvotes: 3

Related Questions