Asked  7 Months ago    Answers:  5   Viewed   47 times

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

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.

Tuesday, June 1, 2021
answered 7 Months ago

Of course, never fails. Found the solution about a minute after posting the above question... solution for those that may have had the same issue:


Found here.

Tuesday, June 22, 2021
answered 6 Months ago

There is no real difference, except that the is helpful when you don't have a direct access to the activity.

In both cases, if not on UI thread, Handler#post(Runnable) will be called behind the scenes.

As CommonsWare mentioned in the comment, there is a difference between the two - when called on Ui thread, Activity#runOnUiThread will call the run method directly, while View#post will post the runnable on the queue (e.g. call the Handler#post)

The important point IMO is that both have the same goal, and for whoever use it, there should be no difference (and the implementation may change in the future).

Saturday, July 10, 2021
answered 5 Months ago

This is fine, and will not cause a memory leak.

As soon as onCreate finishes executing, h will be out of scope and become eligible for garbage collection. If h was static, then you would run into problems. Only when the reference to the context outlives the lifecycle of the context itself will a memory leak occur. A few helpful hints:

  • Use Context.getApplicationContext() when possible. This context will live as long as your application is alive.
  • Be careful when using static fields and inner classes.
  • Run your application through a profiler to check for leaks.
Tuesday, July 13, 2021
answered 5 Months ago

The Roaming folder is copied between machines when roaming profiles are enabled (in a domain environment). Use it for application data that you want to share between machines. But don't store large files in there -- IT departments don't like it when you do that, and it increases the time taken for the user to log in and to log out as the files are copied around.

The Local folder is not copied between machines. Use it for application data that's specific to a machine.

The LocalLow folder is used for low-privilege tasks (such as Internet Explorer). You shouldn't need to worry about it.

For files that the user specifically saved, you should put them (by default) in the Documents folder.

Wednesday, July 14, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :