Difference between Activity Context and Application Context

difference between context and activity
different types of context in android
android get application context from anywhere
what is context in android
cast context to activity kotlin
base context vs applicationcontext
context this
getbasecontext

This has me stumped, I was using this in Android 2.1-r8 SDK:

ProgressDialog.show(getApplicationContext(), ....);

and also in

Toast t = Toast.makeText(getApplicationContext(),....);

using getApplicationContext() crashes both ProgressDialog and Toast .... which lead me to this question:

What is the actual differences between a activity context and application context, despite sharing the wording 'Context'?

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

If you read the docs at getApplicationContext it notes that you should only use this if you need a context whose lifecycle is separate from the current context. This doesn't apply in either of your examples.

The Activity context presumably has some information about the current activity that is necessary to complete those calls. If you show the exact error message, might be able to point to what exactly it needs.

But in general, use the activity context unless you have a good reason not to.

Understanding Context In Android Application, What is the difference between activity context and application context Mcq? In the first place, Activity Context and Application context are totally different objects, so the method parameters where context is used should use ApplicationContext or Activity directly, instead of using parent class Context. In the second place, the doc should specify which context to use or not explicitly.

I found this table super useful for deciding when to use different types of Contexts:

  1. An application CAN start an Activity from here, but it requires that a new task be created. This may fit specific use cases, but can create non-standard back stack behaviors in your application and is generally not recommended or considered good practice.
  2. This is legal, but inflation will be done with the default theme for the system on which you are running, not what’s defined in your application.
  3. Allowed if the receiver is null, which is used for obtaining the current value of a sticky broadcast, on Android 4.2 and above.

Original article here.

What is it about Context in Android?, (for instance, in BroadcastReceiver or Service) Do not use getBaseContext(). In the first place, Activity Context and Application context are totally different objects, so the method parameters where context is used should use ApplicationContext or Activity directly, instead of using parent class Context. In the second place, the doc should specify which context to use or not explicitly.

This obviously is deficiency of the API design. In the first place, Activity Context and Application context are totally different objects, so the method parameters where context is used should use ApplicationContext or Activity directly, instead of using parent class Context. In the second place, the doc should specify which context to use or not explicitly.

What is 'Context' on Android?, Always try to use the nearest context which is available to you. When you are in Activity, the nearest context is Activity context. When you are in  What's the difference between the various methods to get a Context? 119 difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

The reason I think is that ProgressDialog is attached to the activity that props up the ProgressDialog as the dialog cannot remain after the activity gets destroyed so it needs to be passed this(ActivityContext) that also gets destroyed with the activity whereas the ApplicationContext remains even after the activity gets destroyed.

What is the difference between context, activity context and , They are both instances of Context , but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an  set context.getEnvironment() to your service class, then dispatch the appropriate info transferring from the final activity to that thread. You fetched the method, so you interrupt while launching where root is references to the resource you want to detach. Add itself in your application stack and field Set and launch your activity.

Use getApplicationContext() if you need something tied to a Context that itself will have global scope.

If you use Activity, then the new Activity instance will have a reference, which has an implicit reference to the old Activity, and the old Activity cannot be garbage collected.

Which Context should I use in Android? - Ali Muzaffar, Should you use Application context? Ask yourself the following questions: Is the object that is accessing context long living? Holding a reference to Activity in a  Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity. For example - MainActivity. It is an instance of MainActivity only. Application Context

Activity Context and Application Context in Android, Below are the differences between application context and activity Instance of Application context is tied to Application life cycle and gives the context of the  Questions: This question already has answers here: What's the difference between the various methods to get a Context? (8 answers) Difference between getContext() , getApplicationContext() , getBaseContext() and “this” (8 answers) Closed 6 years ago. what is the actual difference between getBaseContext(),getApplicationContext() and activity context which we get by ActivityName.this ?

Mastering Android context, Have you ever encountered this question: What is difference between Application instance as context- Activity - Instance of your activity (this)  Application context and Activity context are the instances of Context but both of them are tied to different life cycles. Instance of Application context is tied to Application life cycle and gives the context of the entire application where an instance of Activity is tied to Activity life cycle.

Difference between base context, application context and activity , getApplicationContext() is the main method virtual method of Context; if you call getContextManager().getAdviceContext() , you'll also run on Context[] and if you  Not quite. An Activity is a Context. And a Context holds information about the application's state, access, permissions, databases, file system, assets, and things like that. Bigger context cover more information about Application. Depends on what you mean about 'bigger'.

Comments
  • This is what I've found stackoverflow.com/questions/1561803/…....
  • This should help clear some things up: Context, What Context?
  • possible duplicate of When to call activity context OR application context?
  • I got a 'java.lang.reflect.InvocationTargetException' when using getApplicationContext, interestingly enough, when I changed to this, it did not crash and work as expected....so if they are both instances of Context, why does one not work and the other does? This info will be of help to others I hope... :) thanks for your prompt answer...
  • I'd need to see the full exception stacktrace to be able to say anything. However, as I said the context instances have different information. Presumably to show a dialog or toast on the screen requires information about the Activity that only the Activity instance has.
  • I would say use the app context unless you have good reason not too (i.e. for dialogs or toasts). It's quite easy to run into memory leaks using activity contexts in different situations so best to be safe :) android-developers.blogspot.com/2009/01/…
  • Dave Smith has posted a very good blog entry for understanding the usage of context, see here. Make sure that you also read the comments!
  • The thing is, that even Dianna Hackborn recommends to use activity context. stackoverflow.com/questions/5228160/… But she seems herself not be totally sure about this.
  • Archive of article web.archive.org/web/20150329210012/https://possiblemobile.com/…
  • what about getting the resources ? i think you better add it up to your table. and you can access resources with applciation context.
  • We can start activity from Application context
  • Article can also be found here: wundermanthompsonmobile.com/2013/06/context
  • Completely agree. Google dropped the ball on this one. It is a complete mess.
  • @SørenBoisen android sdk is a complete mess