Fragment onActivityCreated() is deprecated

Vinod Pattanshetti
2 min readMar 31, 2021

--

When I was working on my project I saw something surprising that strike through on fragment lifecycle method onActivityCreated() that means fragment lifecycle method onActivityCreated() is deprecated.The original purpose of the onActivityCreated() callback was to allow fragment logic to be tied to the creation of its activity.

Let us first see what has the statement has to say as it is in the image below

Image of google framework source code commit

For more info please check this link

https://android-review.googlesource.com/c/platform/frameworks/support/+/1189651

Now what does that mean in simple words? Let me break the decision down for you and answer. Why Android fragment onActivityCreated() is deprecated?

The initial purpose of onActivityCreated() was to get callback from activity to it’s fragments when activity is created, so in onActivityCreated() callback we can do something in fragment. Hence, the need for such thing lead to development of onActivityCreated().

If that was a requirement back then why deprecate now? This is really good question that everybody can think of.

Need for onActivityCreated() deprecation

As technology and our knowledge evolve we learn and recognize the new patterns and anti-patterns in development. We become better than yesterday

In such similar fashion android developers saw the tight coupling of code dependent to the Activity’s life cycle. And they decided that it is not a good practice anymore to be dependent on the activity attach to do certain things inside the fragment.

Most importantly, the clean architecture and inversion of control principle takes its tool on the decision. It is mentioned all the external parameters should be passed in the form of a dependency.

That being said, you should do all your view related initialization in onViewCreated(). And if something else comes up such as mimicking the same behavior as onActivityCreated().

Then what’s the solution? Please check the below code

“To receive a callback specifically when the activity’s onCreate() is complete, a LifeCycleObserver should be registered on the activity’s Lifecycle in onAttach(), and removed once the onCreate() callback is received.”

The below class ActivityLifeCycleObserver is responsible for observing when activiti’s onCreate() is completed.

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

class ActivityLifeCycleObserver(private val update: () -> Unit) : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
owner.lifecycle.removeObserver(this)
update()
}
}

And this is how you will use in method onAttach() of any android fragment

override fun onAttach(context: Context) {
super.onAttach(context)
activity?.lifecycle?.addObserver(ActivityLifeObserver {
// code your stuff here
})
}

That’s all on deprecation of fragment lifecycle method onActivityCreated() and the alternative solution using lifeCycle observer.

--

--

Vinod Pattanshetti
Vinod Pattanshetti

Written by Vinod Pattanshetti

Senior Software Engineer(Android Developer)

Responses (3)