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

How to switch between layouts in ViewPager
Java Jarsigner does not use provided Proxy
migrate from Weblogic to tomcat issue
Fibonacci loop that keeps on asking
“Failed to create task or type antlib:org.apache.maven.artifact.ant:mvn” error in Ant
How to hold a button and record a sound?
Why is the onSharedPreferenceChanged method of this Settings Activity blank?
Unused parameters in Java with array
Java TestNG with Data Driven Testing Across Multiple Tests
jaxb java class to multiple xml mappings
Java HttpRequest get underlying socket?
java inheritance of autowired properties
ActionBarSherlock doesnt worl with AndroidStudio
Complex menu on Toolbar item
java - DateFormat format string for epoch
How to create “The Avery 5160 template”

Categories

HOME
pandas
isabelle
mediawiki
ngrx
plone
v8
upload
adfs
facebook-messenger-bot
elasticsearch-hadoop
vifm
vault
modelica
postgres-xl
n-gram
graphlab
django-admin
windows-7-x64
aurigma
pythonanywhere
immutable.js
kryo
java-3d
tar
sylius
core-text
captiveportal
lcd
firebase-crash-reporting
brunch
virtualdub
ejabberd-module
libuv
quote
srcset
bosh
calibre
hybridauth
restlet
starteam
hue
sas-visual-analytics
g-code
spring-security-kerberos
checkboxlist
user-accounts
outlook-2013
rails-routing
slickedit
tactic
zendesk-app
lync-client-sdk
eventkit
libpng
httplistener
clang-static-analyzer
hover-over
t4mvc
retina
tmuxinator
computer-algebra-systems
directoryservices
block-device
mdt
javafx-webengine
code-access-security
php-5.4
flask-cors
phpthumb
undefined-reference
concurrent-collections
message-driven-bean
centos5
viadeo
fmod
flexmojos
google-cloud-save
blending
jplaton
shellexecute
orchardcms-1.7
lcs
windows-phone-7.1.1
adk
mercurial-server
horizontal-accordion
android-sdk-2.1
sql-server-profiler
floating
xfbml
html-input
fluent-interface
coda-slider
icanhaz.js
gin
data-driven
privilege
temporal-database
rtml
caching-application-block

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