Android Architecture Components : LiveData


Live Data : Life-cycle aware Observables


As an Android developer you often have to work with data that is dynamic and changing with user input, device configuration changes, or even time changes. On top of this as a developer you have to respect the life-cycle of an Activity of Fragment that contains this dynamically changing data: stop updates onStop() or onPause() and restart the updates onStart() and onResume(). Examples of this is location data, countdown timers, user's list selections and any other type of dynamically changing data. Lets see how we can make these tasks easier using LiveData. We will demonstrate LiveData by creating a self contained timer.

Subclass LiveData

LiveData object is set active when the observer's lifecycle is stated or resumed. And the LiveData object is set inactive when the observer's lifecycle is stopped or pause. In our TimerLiveData we start timer when onActive() is called and stop the timer when onInactive() is called. This way the observer only gets timer updates while it is active.

Observe with LifecycleOwner

Instantiate the TimerLiveData object and then observe() in order to get callbacks on timer's value changes but only while our Activity is active.

Benefits of using LiveData

  • Makes objects life-cycle aware
  • Keeps LiveData objects self contained
  • Keeps Activitys and Fragments clean
  • Eliminates memory leaks as LiveData objects clean up themselves.
  • Observers instantly notified of value changes in LiveData

Comments

  1. Appreciating the persistence you put into your blog and detailed
    information you provide.


    Android Training in Chennai


    Android Training in Bangalore

    ReplyDelete
  2. Very interesting and useful presentation to understand the architecture of Android. Thank you.

    Best Android Training institute in Chennai
    Android Classes in Chennai

    ReplyDelete
  3. You’ve written a really great article here. Your writing style makes this material easy to understand.. I agree with some of the many points you have made. Thank you for this is real thought-provoking content
    Python training in bangalore
    Python course in pune
    Python training in bangalore

    ReplyDelete
  4. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  5. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Best Devops Training in pune
    excel advanced excel training in bangalore
    Devops Training in Chennai

    ReplyDelete
  6. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
    Best AWS Training in Chennai | Amazon Web Services Training in Chennai
    AWS Training in Bangalore | Amazon Web Services Training in Bangalore
    Amazon Web Services Training in Pune | Best AWS Training in Pune

    ReplyDelete
  7. There is a desire to earn a lot? Then come to us and win. top slot machine games online .Fate favors you, come in and win.

    ReplyDelete
  8. Качественный декоративный алюминиевый led профиль я обычно беру в компании Ekodio, советую.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Data Binding : Dynamic RecyclerView Adapter

Android Data Binding : Tricks and Magic