https://kotlinlang.org logo
#android
Title
# android
c

codeslubber

10/02/2017, 6:43 PM
ok nm this works (and is quite pretty):
Copy code
conversionViewModel?.getConversion()?.observe(this, Observer<Conversion>{ conversion ->
            println("observer called with conversion: $conversion")
        })
and simple.
a

arekolek

10/03/2017, 9:45 AM
You can also do:
Copy code
conversionViewModel?.getConversion()?.observe({ lifecycle }) {
    println("observer called with conversion:  $it")
}
👍 1
btw, normally you’d be able to
observe(this) { print(it) }
, but for some reasons mentioned here https://stackoverflow.com/q/46542037/1916449 in this case it doesn’t work
I guess it might be better to use the objects version (yours) instead of lambda version (mine), because the latter will generate an extra anonymous class that just forwards the call to the lifecycle owner
👍 1
c

codeslubber

10/04/2017, 2:11 PM
I actually like the object version though it’s a bit more verbose. Bottom line is the fact that these listeners can be wired up to a real viewmodel that is natively supported is the great thing here.
9 Views