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

Jai*_*dra 0 android activity-finish android-activity

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?

sda*_*bet 8

大部分时间它是相同的,除非你在一个内部阶级.

在这种情况下,第二种表示法用于消除对包含活动的方法的调用.

例如:

@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
        }
    };
}
Run Code Online (Sandbox Code Playgroud)