7

What is the difference between getApplicationContext() and getActivity() and this in Android?

AppB
  • 121
  • 1
  • 1
  • 5
  • 3
    Possible duplicate of [What is the difference between getActivity() and getApplicationContext() in android?](http://stackoverflow.com/questions/28385610/what-is-the-difference-between-getactivity-and-getapplicationcontext-in-andr) – mehrdad khosravi Jun 06 '16 at 10:53
  • 1
    Why don't you Google before asking..??http://stackoverflow.com/a/33447949/6127411 – Janki Gadhiya Jun 06 '16 at 10:54
  • Refer following link:- http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and – User6006 Jun 06 '16 at 10:54

2 Answers2

17

There is a lot of difference between :

View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.

Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().

Also check :

https://stackoverflow.com/a/10641257/4018207 https://developer.android.com/reference/android/view/View.html#getContext%28%29

Community
  • 1
  • 1
AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
2

There is huge difference. An android application can have more than one activity, when you say getApplicationContext(), it gives you the context of entire application: see details :

However when you say getActivity() it just gives you the instance of activity which you are currently in.

Community
  • 1
  • 1
Ashish Rawat
  • 5,541
  • 1
  • 20
  • 17