aryaxt
aryaxt

Reputation: 77596

Kotlin inner class accessing outer class?

How do I call a method in outer class from the inner class? I can pass it as context, but I can't call methods on it

    loginButton.setOnClickListener {
        ssoManager.login(emailEditText.text.toString(), passwordEditText.text.toString())
                .subscribe(object: Consumer<SSOToken> {
                    val intent = Intent(this@LoginActiviy, PasscodeActivity::class.java)
                    [email protected](intent)
                })

Upvotes: 1

Views: 1751

Answers (1)

zsmb13
zsmb13

Reputation: 89548

I'm not sure what APIs you're using here, I'm gonna assume that your Consumer is java.util.function.Consumer for the sake of the answer.

You are writing code directly in the body of your object, and not inside a function. The first line of creating the Intent only works because you're declaring a property (and not a local variable!).

What you should do instead is implement the appropriate methods of Consumer, and write the code you want to execute inside there:

loginButton.setOnClickListener {
    ssoManager.login()
            .subscribe(
                    object : Consumer<SSOToken> {
                        val foo = "bar" // this is a property of the object

                        override fun accept(t: SSOToken) {
                            val intent = Intent(this@LoginActiviy, PasscodeActivity::class.java)
                            [email protected](intent)
                        }
                    }
            )
}

Upvotes: 4

Related Questions