Search code examples
androidandroid-contextandroid-application-class

Android - Using this or getApplicationContext() in MyApplication class?


I have created a MyApplication class that extends Application and have defined it in the manifest.

Within MyApplication's onCreate() method, is there any difference between using this or getApplicationContext() in order to get the application context?

(Presumably, the latter just exists because Application is a Context, but I just need to be sure.)


Solution

  • Your class MyApplication inherit indirectly from the Context class (Applicaiton > ContextWrapper > Context). So when you use the reference this as Context instance, you are just using the polymorphism concept.

    is there any difference between using this or getApplicationContext() in order to get the application context?

    There is no difference.