Whenever you call
myLiveData.observe(this, Observer { ... })
where
this
is your
Activity
or
Fragment
, you're ensured that the
Observer
is tied to the lifecycle of that Activity/Fragment, i.e. it will unregister itself in
onDestroy()
.
Otherwise, it will be unsafe to reference a
Context
in that
Observer
if you register it with
myLivedata.observeForever(observer)
, unless you unregister that observer with
removeObserver(observer)
(but if you do that in a Fragment, why wouldn't you use
observe
instead ?)