In which lifecycle method , we should observe a livedata object?

Viewed 186

In which method should you observe a livedata object?

  • onResume()
  • onRestart()
  • onCreate()
  • ondestroy()

please include reason as well.

1 Answers

Observe LiveData objects In most cases, an app component’s onCreate() method is the right place to begin observing a LiveData object for the following reasons:

To ensure the system doesn’t make redundant calls from an activity or fragment’s onResume() method. To ensure that the activity or fragment has data that it can display as soon as it becomes active. As soon as an app component is in the STARTED state, it receives the most recent value from the LiveData objects it’s observing. This only occurs if the LiveData object to be observed has been set.

Related