What is the difference between passing this and ClassName.this from the event handler when passing to the int constructor?

In the previous Android programming tutorial, page 192, we see the implementation of LunchList#onOptionsItemSelected . Inside this implementation, we see two startActivity passed to startActivity : one whose constructor is passed to LunchList.this , the other whose constructor is passed to this .

What's the difference?

See lines 78 - 91 here . Note that onOptionsItemSelected not declared inside the inner class.

On Android / Java, does this value change in the context of event handlers or function bindings through reflection? Can Intent this tags pass?

+7
source share
3 answers

In Java, this refers to an contained class, and ClassName.this refers to the first containing class, whose name is ClassName . Event handlers are usually written as anonymous inner classes, so if you want to access an event handler that contains a class (and not an event handler class), you need to specify ContainingClass.this , not this .

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

+10
source

When you create an Intent inside an Inner Class , use ClassName.this (here className must be the name of an Activity class), and when creating an Intent inside an Activity class, you can use this .

+2
source

In this example, this does not matter, because in any condition the class that starts the activity will remain the same. The first Intent parameter refers to the context of the class from which the activity will be launched, and where the packet data will be transferred from.

+1
source

All Articles