-4

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.)

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • in your application subclass `this` and `getApplicationContext()` are the same – Blackbelt Jan 25 '18 at 13:27
  • @Blackbelt, and that is not always true. `getApplicationContext()` not always returns an instance of the application. – Vladyslav Matviienko Jan 25 '18 at 13:29
  • @VladMatvienko, the documentations disagrees with you – Blackbelt Jan 25 '18 at 13:30
  • @Blackbelt, sorry, but looks like you are using non-official Android documentation. Because this: https://developer.android.com/reference/android/content/Context.html#getApplicationContext() `getApplicationContext` returns a `context of the single, global Application object of the current process`, not an application object itself. In most cases they are the same, but not in **all cases**. Reffer to https://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext – Vladyslav Matviienko Jan 25 '18 at 13:35
  • 2
    The question was about the `getApplicationContext()` *inside* the `MyApplication` class that extends the `Application` - and there they will be the same. – Sasa Sekulic Jan 25 '18 at 13:39
  • Why downvote this question? What's wrong with it? Do some people choose to downvote simply because they know the answer when the asker doesn't?! And if that's the case, then why not downvote every single question you know the answer to rather than leaving an answer - which would actually help, which is what Stack Overflow is meant to be all about, right?? – ban-geoengineering Jan 25 '18 at 13:51
  • Or should each question be prefixed with "I've been searching google for hours, but can't find the answer..." ?! – ban-geoengineering Jan 25 '18 at 13:53
  • @ban-geoengineering Did my answer help you ? (I think your question isn't wrong at all) – François Legrand Jan 25 '18 at 15:34

1 Answers1

1

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.

François Legrand
  • 1,151
  • 1
  • 14
  • 25