相关文章推荐
深情的煎鸡蛋  ·  the value of the ...·  1 周前    · 
伤情的刺猬  ·  java - Exception in ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have this piece of Java code:

MaterialDialog builder = new MaterialDialog.Builder(MainActivity.this)

I want to get the MainActivity object in Kotlin. The automatic conversion breaks at MainActivity.this.

And if this code is not inside MainActivity, then which instance of MainActivity is it connected to (and how)? Which instance do you want to access as this? – hotkey Jan 12, 2017 at 15:34 The working Java code references just the activity.this, so a static one? I'm not sure tho. – Rado Jan 12, 2017 at 16:50 How is this code called, if not in MainActivity but still casting this to MainActivity? In a subclass maybe? – Marc Plano-Lesay Jan 13, 2017 at 7:18

You can get a reference to your MainActivity object in Kotlin by using a qualified this. e.g.:

class MyActivity : MainActivity() {
    val builder = MaterialDialog.Builder(this@MyActivity)
                How about Fragment? I've injected Context into Fragment(with @ActivityContext attribute), now I need Activity. Of course, I know about getActivity() (Kotlin activity) in the Fragment, but is the second way okay or cause I have Context, it's better to use it to access to the activity?
– Dr.jacky
                Aug 24, 2018 at 15:37
                @Dr.jacky I think you can do ' (context as? YourActivity)' cast. Now you can access your activity methods
– Hanako
                Feb 13, 2019 at 19:47
                @siddarth-g if you are having an issue with unresolved references then I recommend creating a new question with an MCVE to get help.
– mfulton26
                Dec 27, 2019 at 14:17
                @mfulton26 its not compilation issue this@ActivityName did not work for me in another project as well.
– Siddarth G
                Jul 26, 2020 at 8:40

Answer is: this@ActivityName

For example: You should use it if you would like to define "Context" in MainActivity.kt

var mContext:Context = this@MainActivity

Why? Because in Kotlin language @ has mean "of" such as:

val a = this@A // A's this

If you want to learn more information, you can look Kotlin Language website: This Expression in Kotlin

If you are calling Activity.this from an inner class, you have to put inner before the class

class MyActivity : MainActivity() {
    // Call from class itself
    val builder = MaterialDialog.Builder(this@MyActivity) 
    inner class Inner {
        this@MyActivity // Call from the inner class 
                @Allen you are correct, inner keyword is required to access the Activity inside inner class
– droidev
                Feb 18, 2020 at 7:02
                are you adding this comments for  what? Can you give some resolved reference? It will helpful to us, So that we will follow it from next time
– The Bala
                Jan 13, 2020 at 10:30

You can get the object of activity like this.

class DemoActivity : BaseActivity() {
    val builder = MaterialDialog.Builder(this@DemoActivity)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.