What is the difference between finish() and ActivityName.this.finish()?

 ̄綄美尐妖づ 提交于 2019-12-12 04:36:00

问题


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

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