问题
Is there any difference between finish()
and ActivityName.this.finish()
? If we have activity with name SampleActivity
, we can finish it by calling finish()
and by SampleActivity.this.finish()
. What is the difference?
回答1:
Most of the time it's the same except if you are inside an inner class.
In that case the second notation is used to disambiguate calls to the containing activity's methods.
For instance:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish(); // the activity's finish()
new Runnable() {
private void finish() {
...
}
@Override
public void run() {
SampleActivity.this.finish(); // the activity's finish()
finish(); // the runnable's finish()
}
};
new Runnable() {
@Override
public void run() {
SampleActivity.this.finish(); // the activity's finish()
finish(); // the activity's finish() (because the inner class doesn't hide it
}
};
}
来源:https://stackoverflow.com/questions/13394136/what-is-the-difference-between-finish-and-activityname-this-finish