What's the difference between passing this vs. ClassName.this from an event handler when passed to Intent constructor?

痴心易碎 提交于 2019-12-03 12:57:24

In Java, this refers to the containing class, and ClassName.this refers to the first containing class whose name is ClassName. Event handlers are typically written as anonymous, inner classes, so if you want to refer to the event handler's containing class (and not the event handler's class), you need to specify ContainingClass.this, not this.

Reference: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.4

When you are creating an Intent inside an Inner Class use ClassName.this(here className must be the Activity class Name) and if creating Intent inside an Activity class you can use this.

For this example it will make no difference as in either condition the class that will start the activity will remain same. First parameter of the Intent refers to the context of the class from where the activity will be launched and from where the bundle data will be passed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!