0

I am currently developing an Android application that uses the Retrofit library for REST api usage.

For instance, I have the following code from MainActivity.kt :

fun userLogin(){
    calls.userLogin() { updateUiComponents() }
    }

fun updateUiComponents(){
    Toast.makeText(applicationContext, "LAMBDA EXECUTED",Toast.LENGTH_SHORT).show()
    }

And I have in a separate file the definition of the Retrofit calls:

fun userLogin(postActionMethod: () -> Unit){
    val call = service.userLogin()
    call.enqueue(object : Callback<LoginResponse>{
        override fun onFailure(call: Call<LoginResponse>?, t: Throwable?) {
            Log.i("ERROR RUNNING CALL", t?.message.toString())
        }

        override fun onResponse(call: Call<LoginResponse>?, response: Response<LoginResponse>?) {
            postActionMethod()
        }
    })
}

After the Retrofit call is implemented and it is successful, reaching the onResponse method, I would like to send the Response object as a parameter of the lambda function back to the MainAcativity.kt. From the MainActivity.kt, the lambda function would use this information to perform some specific task.

Is that a way of defining a lambda function like this, with arguments? If it is the case, how can I pass the lambda function as a parameter like done on the following line:

calls.userLogin(body) { updateUiComponents() }

Thank u!

0

1 Answer 1

2

I don't know if I get what your problem is but a lambda does not need to do not have any parameter. You can easily do something like

fun userLogin(postActionMethod: (Response<LoginResponse>?) -> Unit){
    val call = service.userLogin()
    call.enqueue(object : Callback<LoginResponse>{
        override fun onFailure(call: Call<LoginResponse>?, t: Throwable?) {
            Log.i("ERROR RUNNING CALL", t?.message.toString())
        }

        override fun onResponse(call: Call<LoginResponse>?, response: Response<LoginResponse>?) {
            postActionMethod(response)
        }
    })
}

so you consume it with

fun userLogin(){
    calls.userLogin() { updateUiComponents(it) }
    }

fun updateUiComponents(response: Response<LoginResponse>?){
    Toast.makeText(applicationContext, "LAMBDA EXECUTED",Toast.LENGTH_SHORT).show()
    }
Sign up to request clarification or add additional context in comments.

4 Comments

That's exactly what I need! I tried the change you commented but it did not work. The problem is exactly at this line: calls.userLogin() { updateUiComponents(it) ... It requires that I pass the response parameter on this line and using "it" as you commented did not work :/
You can use it to refer to the only param of a lambda. I just wrote an equivalent code and it worked for me
Sorry, I forgot to update the code from fun userLogin(postActionMethod: (Response<LoginResponse>?) -> Unit) . And if I want to pass more than one parameter, I can keep the same syntax, right? For example: postActionMethod(response1, response2)
Yes, you probably want to check the Higher-Order Functions documentation. If the answer solved your problem consider marking it as correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.