The activity life cycle components provide several callbacks which are used to create, stop, or resume an activity, or destroy the process in which the activity.
In activity lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, we make a streaming video player, you need to stop the video and terminate the network connection when the user switches to another app from the existing app.
The main use is that each callback allows you to perform a specific task that’s appropriate to a given change of state in the entire application.
Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy() this is most important for developed.
The below Screenshot shows how the activity lifecycle actually works
OnCreate() Called when the activity is first created.
OnStart() Called when the activity becomes visible to the user.
OnResume() Called when the activity starts interacting with the user.
OnPause() Called when the current activity is being paused and the previous activity is resumed.
OnStop() Called when the activity is no longer visible to the user.
OnDestroy() Called before the activity is destroyed by the system(either manually or by the system to conserve memory)
OnRestart() Called when the activity has been stopped and is restarting again.
class MyActivity : Activity(), LifecycleOwner {
private lateinit var lifecycleRegistry: LifecycleRegistry
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleRegistry = LifecycleRegistry(this)
lifecycleRegistry.markState(Lifecycle.State.CREATED)
}
public override fun onStart() {
super.onStart()
lifecycleRegistry.markState(Lifecycle.State.STARTED)
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
}
Customization of the activity life cycle components is important for every activity and fragments are now subclass LifeCycleObserver, you can get an instance of their Lifecycle with the call function and give that Lifecycle instance to the LifecycleObserver class. That is the way you set up a lifecycle observation. Kotlin offers a modern and powerful language alternative for Android app development.
I would like to conclude this blog is used for access when using any media player rather than switching between any activity/fragment to another activity/fragment.
There is no guarantee that app components start before the activity/fragment stop. this long running operation so some configuration checks in onStart().This can cause a onStop() method finishes before onStart().
Yes, you can make it.
You can use this custom life cycle component to fix this issue.
Using dynamic packaging technology, one can encapsulate flights, accommodation, cars,…
In today’s contemporary era of accelerated digital evolution, the importance…
The fact is that in today’s internet environment the only…
This website uses cookies.