简体   繁体   中英

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)
                    this@LoginActiviy.startActivity(intent)
                })

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)
                            this@LoginActiviy.startActivity(intent)
                        }
                    }
            )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM