1

I am seeing code like this following:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);

For example, in this question, this question and many other more.

But is it necessary to have the class name MainActivity in front of the this pointer? I remove it and don't see any differences in the app at all. As a matter of fact, when I am reading the documentation, the Acitivity name is not being used in front of the this.

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234

1 Answers1

3

No it's not necessary except in certain cases. Those cases being when you need to refer to an outer instance from within a nested class.

For example, if you had an anonymous AsyncTask within your activity you would need to use the MainActivity.this style to refer to the activity instance.

Keyword for the outer class from an anonymous inner class

Community
  • 1
  • 1
Ian Newson
  • 7,679
  • 2
  • 47
  • 80