Mobile ApplicationSolutions

Custom Activity Life Cycle Components for Android

Introduction

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.

Why is it Important to Develop Mobile Apps?

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.

Prerequisite:

  • Android Device, Windows/Linux
  • Tech Stack, We’re using: Kotlin, java, and XML language
  • Tools we are using: Android studio
  • Java Development Kit
  • Android Studio 3.0 or later.
  • Android SDK API Level 16 or higher.
Hire Dedicated Developer

Process of Implementing Activity Life Cycle in Android

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

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.

Implementing Custom Lifecycle Components

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
    }
}

Why is the customization of life cycle components important?

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.

See also  20 Innovative App Development Ideas for 2024

Conclusion

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().

FAQs

Can I make a custom UI?

Yes, you can make it.

Why do media or youtube players play when switching any activity/fragment?

You can use this custom life cycle component to fix this issue.

lets start your project

Related Articles