lobiprofit.blogg.se

Android life cycle components
Android life cycle components








android life cycle components

It should not be a concern for end users of our library i.e. The beauty of such separation of concern can be also felt when we are developing some library and we intend it to be used as a third party library.

#Android life cycle components code#

Our VideoActivity no longer needs to bother about calling its dependent components methods to pause or play in its life cycle callback methods which makes the code clean, manageable and testable. Our VideoPlayer class logic is separated from VideoActivity. Well it makes things quite resilient and isolated. So we have our VideoPlayer, instance of LifecycleObserver, and we need to add this as an observer to the Lifecycle object of VideoActivity. This Lifecycle object is observable and notifies its observers about the change in state. Lifecycleowner interface is a single method interface which contains a method, getLifecycle(), to get the Lifecycle object corresponding to its implementing class which keeps track about the life cycle state changes of activity/fragment or any other component having a life cycle. Well, this binding is quite easy, VideoActivity is an instance of 7.app.AppCompatActivity which implements Lifecycleowner interface. We need some binding between this VideoPlayer class and the VideoActivity so that our VideoPlayer object gets notified about the life cycle state changes in VideoActivity. Using the specific annotations with the VideoPlayer class methods it will be notified about the life cycle state changes in VideoActivity. Once we have integrated the arch components we can make our VideoPlayer class implement LifecycleObserver, which is an empty interface with annotations. To keep things simple we can add the below lines to our app gradle file to add life cycle components from android.arch library So let’s dive and see how we can implement the above example using life cycle aware components.

android life cycle components

The package provides classes and interfaces that prove helpful to solve such problems in an isolated way. Thus, the code becomes clean, maintainable and testable. Our activities or fragments no longer need to play with these component logic and can focus on their own primary job i.e. With the introduction of life cycle aware components in library, we can move all this code to the individual components.

android life cycle components

Now imagine if we add separate components for audio, buffering etc, then our VideoActivity has to take care of all these components inside its life cycle callback methods which leads to poor organization of code, prone to errors. When we analyze this code we can see that even for this simple application our activity has to take a lot of care about calling the play, pause and stop methods of VideoPlayer class. Thus we will have the following code in VideoActivity’s onDestroy() method So we will have the following code in our VideoActivity’s onResume() and onPause() methods.Īlso, we would like it to stop playing completely and release the resources when the activity gets destroyed. Now as for any video player we would like it to play the video when VideoActivity is in foreground i.e, in resumed state and pause the video when it goes in background i.e when it goes in the paused state. Our VideoActivity creates an instance of this VideoPlayer class in onCreate() method Where we have an activity named as VideoActivity which contains the UI to play the video and we have a class named as VideoPlayer which contains all the logic and mechanism to play a video. Let’s say we are developing a simple video player application.

android life cycle components

A life cycle aware component is a component which is aware of the life cycle of other components like activity or fragment and performs some action in response to change in life cycle status of this component.










Android life cycle components