java


How does finish() work in OnClick event?


I have an Activity with one button to quit the Activity.
Through the layout.xml I have to set the OnClick event to cmd_exit and a call to 'this.finish()' works fine
public void cmd_exit(View editLayout){
this.finish();
}
, but when I add a OnClickListener instead
cmd_exit = (Button) this.findViewById(R.id.cmd_ExitApp);
cmd_exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
this.finish() gives error. It has to be only finish().
I understand that finish() lives in the Activity class so my question is how is it working in the second snippet?
When you've created an OnClickListener as an anonymous class, the this keyword refers to that listener instance rather than the enclosing class. You could use YourActivityClassName.this instead. You could also just omit the this, and since OnClickListener doesn't have a finish() method so the Activity one will be used.
In the first case this refers to the Activity instance. In the second case you have an anonymous inner class instantiated with new View.OnClickListener() whose this refers to the instance of the class. Each anonymous inner class has an implicit reference to the outer class it's been instantiated in. That reference is implicitly used when calling finish(), i.e. OuterClassName.this.finish().
How is it working in the second snippet?
The compiler is responsible for passing a reference to the outer class instance into the inner class. It modifies each of the inner class' constructors by adding the reference to the outer class instance as a constructor parameter.
Reference: Core Java Volume I - Fundamentals, 9th Edition, Chapter 6.4: Inner Classes, page 309
As #Gabe Sechan pointed out, in order to refer to the outer class within the anonymous inner class use OuterClassName.this. Reference: How do you get a reference to the enclosing class from an anonymous inner class in Java?.

Related Links

Java ThreadPool that allocates work by thread name
jackson jersey json : serializing date from java to json
findViewById problems for webview in navigation drawer
For loop how to print to console without leaving on first iteration java
Dispatcher servlet throws exception: NumberFormatException
Overriding equals method to accept doubles inside of Object class. Java
Line break between GWT widgets
How to set shift in String.format dynamically? [duplicate]
Java audio doesn't play in jar file
How can I obtain the -D parameters passed in to Java launch
Set multiple listeners to one method
why one class of same package can't access another?
cannot find symbol : class NumberTools
Java apache POI java.lang.IllegalArgumentException: Position 21504 past the end of the file
Selenium Automatically Closes Chrome Console (attached or window)
How to check values in collection for uniques by some criteria using Java 8

Categories

HOME
gitlab
hive
gerrit
gremlin
dictionary
mfc
google-play
jira
analysis
tizen-web-app
rdf
survey
yum
mouse
slurm
python-unittest
maude-system
user-input
etl
resize
ups
netflix
django-admin
reactive-cocoa
connection-string
predix
opencover
mps
restful-authentication
user-interaction
phpfox
fifo
skia
http-referer
jmonkeyengine
overlap
io-redirection
restlet
jquery-bootgrid
android-browser
hilbert-curve
jna
azure-ml
janrain
spring-security-kerberos
isbn
keydown
http-redirect
auto-update
angular-resource
menuitem
fault
revapi
atomicity
logparser
hls.js
tizen-native-app
lowpass-filter
lift-json
boost-preprocessor
python-cryptography
account-kit
pycaffe
jquery-filter
moveit
superstack
rvest
suffix-tree
rhino-servicebus
jfugue
collapse
cannon.js
atk4
lustre
fscommand
nstableviewcell
fluentautomation
hyprlinkr
imdbpy
c18
ms-project-server-2010
mhtml
dmoz
bitsharp
bluepill
uiviewanimation-curve
cufon
heartbeat
subgurim-maps
recent-documents
calling-convention
inotifycollectionchanged
pyinotify
xfbml
remember-me
cxxtest
preference
web-architecture
photoshop-cs4
exchange-server-2003
multiple-languages
commodore

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App