Live Data in android

Live Data in android

In Android, "Live Data" is a data holder class that is used to observe changes in data and react to them accordingly.

You can use live data to build reactive user interfaces that automatically update in response to changes in your application's data.

Here are the basic steps to using live data in your Android app:

  1. Add the Android Architecture Components library to your project's build.gradle file.
  2. Create a live data object to hold your app's data. You can create a live data object by extending the MutableLiveData class.
  3. Observe the live data object in your app's UI components. You can use the observe method to set up an observer for your live data object. The observer will be notified whenever the live data object's value changes.
  4. Update the live data object whenever your app's data changes. You can update the value of a live data object by calling its setValue or postValue methods.


class MyViewModel : ViewModel() {
? ? private val _items = MutableLiveData<List<String>>()
      val items: LiveData<List<String>>
      get() = _items
      fun loadItems() {
          // Load items from a data source
          _items.value = listOf("Item 1", "Item 2", "Item 3")
         }
      }
      class MyFragment : Fragment() {
        private lateinit var viewModel: MyViewModel
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      
      // Inflate the layout for this fragment
      val view = inflater.inflate(R.layout.my_fragment, container, false)
     
      // Set up the view model and observe the items live data
      viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
      viewModel.items.observe(viewLifecycleOwner, Observer { items ->
      // Update the UI with the new list of items   
      })
      return view

      }?
}?        


In this example, the MyViewModel class defines a live data object called _items that holds a list of strings. The loadItems method updates the value of _items whenever new items are loaded from a data source.

The MyFragment class observes the items live data in its onCreateView method. Whenever the value of items changes, the observer's onChanged method is called with the new list of items. The UI can then be updated with the new list of items.

Note that live data objects should only be updated on the main thread. If you need to update a live data object from a background thread, use the postValue method instead of setValue.

要查看或添加评论,请登录

Sajjad Ahmed的更多文章

  • Mental Health Needs Attention

    Mental Health Needs Attention

    ?? Hi there, as a developer, I believe it's essential to talk about mental health and bring more attention to it…

  • Write different ways to achieve concurrency in iOS

    Write different ways to achieve concurrency in iOS

    Concurrency is the ability of a program to perform multiple tasks simultaneously. In iOS, there are different ways to…

  • Different types of application state in IOS

    Different types of application state in IOS

    In iOS development, there are three main types of application state that refer to the state of an app at any given…

  • Opaque types explained with code in swift

    Opaque types explained with code in swift

    What are opaque types? Opaque types allow you to describe the expected return type without defining a concrete type. A…

  • Synchronous and asynchronous IOS

    Synchronous and asynchronous IOS

    Synchronous programming is when code is executed in a blocking manner. This means that when a function is called, the…

    1 条评论
  • Delegates in swiftui

    Delegates in swiftui

    In SwiftUI, you can use delegates to communicate between views or components. A delegate is an object that conforms to…

    1 条评论

社区洞察