Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin syntax for LiveData observer?

Tags:

android

kotlin

I have the following bit of code in my HomeActivity to use LiveData.

override fun onCreate(savedInstanceState: Bundle?) {     super.onCreate(savedInstanceState)      // Show the launch splash screen.     //     this.setContentView(R.layout.activity_home)      this.viewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)      this.viewModel.getUser().observe(this, Observer { user: User? ->      });  } 

While this seems to work, what does the following part mean?

Observer { user: User? ->  } 

This must result in an object that conforms to the Observer interface which has

void onChanged (T t) 

https://developer.android.com/reference/android/arch/lifecycle/Observer.html

How does

Observer { user: User? ->  } 

result in an object with an onChanged method?

I don't know what putting the name of an interface in front of a lambda expression means.

Thanks!

like image 846
Ted Henry Avatar asked Dec 02 '17 05:12

Ted Henry


People also ask

How do I use LiveData Observer?

Attach the Observer object to the LiveData object using the observe() method. The observe() method takes a LifecycleOwner object. This subscribes the Observer object to the LiveData object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment.

How do I initialize kotlin LiveData?

“how to initialize livedata kotlin” Code Answerval liveData = MutableLiveData<String>(). default("Initial value!")


Video Answer


2 Answers

This is called SAM Conversion, a concept that helps interacting with Java Single Abstract Method Interfaces like in your example.

The following creates an implementation of Runnable, where the single abstract method is run():

val runnable = Runnable { println("This runs in a runnable") } 

It’s described in the docs: https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions

Alternatively, but more verbose, would be to use an object:

val runnable2 = object : Runnable {         override fun run() {             println("This runs in a runnable")         } } 

Both are examples of anonymous implementations of that interface. It's of course also possible to create a concrete subclass and instantiate it then.

class MyRunnable : Runnable {     override fun run() {         println("This runs in a runnable")     } }  val runnable3 = MyRunnable() 
like image 156
s1m0nw1 Avatar answered Sep 23 '22 07:09

s1m0nw1


in Kotlin the Observer { } lambda gives you param it, you can rename it as you want and use. by default data will be available with it.something() etc...

JAVA:

... new Observer {   void onChanged(User user){      user.something()   } } 

KOTLIN

... object : Observer<User> {    fun onChanged(user: User){         user.something()    } } 

OR

... Observer {    it.something() } 

you can rename it to whatever you want like

... Observer { myUser ->    myUser.something() } 
like image 41
Amir Raza Avatar answered Sep 20 '22 07:09

Amir Raza