What is the difference between getContext() , getApplicationContext() , getBaseContext() , and “this”?

Technology CommunityCategory: AndroidWhat is the difference between getContext() , getApplicationContext() , getBaseContext() , and “this”?
VietMX Staff asked 3 years ago
  • 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().
  • this is refer current class object always. this and getContext()are not always same e.g. in Activity class, you can use this because Activity inherits from Context but method getContext() is not in Activity class.