As we know the main problems in Android development is with lifecycle management issues. Means unable  to handle application lifecycles appropriately can create some  issues such as memory leaks and some crashes. Lifecycle-aware components which is a part of Android Architecture Components complete tasks in response to a change in the lifecycle status of another view(Activities and fragments). These components will help you create life cycle aware app, that is easier to maintain and test. So we can consider Android Jetpack Lifecycle-aware components as an advanced approach that help us for managing lifecycles in Android development.

Android Architecture Components is a set of libraries which is provided by Google, which  help us  to design Android applications that are testable, maintainable and robust . In some other words, this component helps us in handling data across lifecycle events and  during configuration changes. It also help in manual handling of callbacks and memory leak.

Basically, most of the Android components have a lifecycle which are associated with that component. You have  to be careful about for handling your app’s lifecycle, which is  not sometime is an easy task when for multiple asynchronous calls in a simultaneous way. Failing in the handling of application lifecycles mostly can arise some problematic issues such as some crashes and memory leaks. If you can manage the lifecycle correctly, implementing all of its lifecycle-dependent code in your lifecycle methods that is onStart() and onStop() methods which is  complicated. In short, this approach makes your code very difficult to  understand, maintain, and test.So, the Lifecycle aware component help us to manage lifecycle by implementing of its  classes and interfaces. which  help in adjust their behavior automatically.

What is lifecycle aware component?

A life cycle aware component could be a component which is take care of the life cycle of other components that is our view (activity or fragment )and performs some action in response to vary in life cycle status of this component. In other word an object is said to be as lifecycle-aware if it is capable to detect and respond to changes in the lifecycle state of some other objects within an app, like LiveData are already lifecycle-aware. 

Lifecycle-aware component is decided into three main classes-

LifeCycle

A LifeCycle is an object that fetch information about the current lifecycle of a LifeCycleOwner that is Fragment or Activity, and provides information in the form of Events and States.

Events

The Events are dispatched with reference to the LifeCycleOwner events like onCreate(), onStart(), onStop(), onResume() .

State

State of life cycle could be For onCreate(), state of event is made, For onStart(), state of event is Started, for onPause(), state of event is Paused and onStop() state of event is Stopped and similarly for onDestroy, state of event is Destroyed, so each event of lifecycle there’s state.

LifeCycle Owner

Lifecycle owners are those who has lifecycle or own the life cycle (such as activity and fragment classes) are lifecycle owners.

class ReduceComplexComponent : LifecycleObserver{

    registerLifecycle(lifecycle : Lifecycle){
       lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun resume() {
       Log.d("OnResume","ON_RESUME")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun pause() {
       Log.d("onPause","ON_PAUSE")
    }
}

LifeCycle Observer

It are those component which observable the life cycle of other component. To observe the state of a lifecycle owner it has to implement the LifecycleObserver interface and contain event listener handlers for change events of any lifecycle it must observe.

class MainActivity : AppCompatActivity(), LifecycleObserver {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ReduceComplexComponent().registerLifecycle(lifecycle)

    }

Using arch.lifecycle 

As life cycle aware components introduce in android.arch.lifecycle library, Now we can move all code to the individual components and our activities or fragments no longer to have  these component logic and can focus on maintain UI. Thus, the code becomes clean, maintainable and testable. The android.arch.lifecycle package gives classes and interfaces that are helpful to solve such problems in an isolated way.

How do Lifecycle states move in Activities?

Before execution of any lifecycle methods, the view(Activity/Fragment) state is simply INITIALIZED. After that, the state is moved automaticully in its lifecycle methods. The state is CREATED upon onCreate() method, and RESUMED upon onResume() method, Here there is no PAUSED state  or STOPPED state , here lifecycle methods simply reduce the state like onPause() moves the state from RESUMED to STARTED.

There is  a special case for the ON_STOP method of lifecycle event. This method is triggered in the activity’s onStop() and also triggered onSaveInstanceState(). This is used to prevent any inconsistencies in the navigation state of your app, as we have a gap between when onSavedInstanceState() and onStop() are called.

Some use cases for lifecycle-aware components

Lifecycle-aware components can help you to manage lifecycles in a various  case. For instance:

  • For Stop and startvideo buffering
  • For Start and stop network connectivity.
  • For Pause and resume animated drawables.

Some best practices for lifecycle-aware components by Google

  • Always Keep your UI controllers (activities and fragments) as lean as possible by using  of LiveData and ViewModel
  • AlwaysPut your data logic in your ViewModel class. Basically, ViewModel should  be use as a connector between your UI controller and the rest of your app.
  • Always Use Data Binding to maintain a clean connection between your views and the UI controller.
  • Must avoid referencing a View or Activity context in your ViewModel.

Conclusion

when a view(Activity/Fragment)  depends on some network api call which is handled by a network manager class. We have to make that network manager class life cycle aware so that it can to supply the data to activity only when it is active or help in not to keep a reference to view(Activity/Fragment) when it is destroyed. Thus, help to prevent memory leaks. So now we can develop a well managed life cycle aware app using the life cycle aware components which is provided by android.arch.lifecycle package. By using this The code will be loosely coupled and easy for maintain, testing and debugging .

Note

  • While creation phase activity’s method calls first then call observer’s methods
  • While in destruction phase observer’s methods calls first then call activity’s method

What is Android Jetpack

You may also like...

0 Comments

No Comment.