0

could anyone please tell me what's the difference between getApplicationContext() and MyActivity.this.."MyActivity" being a class that extends Activity class..somethimes getApplicationContext() doesn't work and I need to use MyActivity.this when the constructur asks for a context...
for example, if i use ProgressDialog p=new ProgressDialog(getApplicationContext()); it gives an error
however, if i use ProgressDialog p=new ProgressDialog(MyActivity.this); it runs fine

2
Contributors
4
Replies
12
Views
4 Years
Discussion Span
Last Post by cool_zephyr
0

Application context getApplicationContext() is different then View context getContext or as in your case MyActivity.class (both means same). Real difference in usage is only what part of lifecycle of the activity or whole application you will be affecting with whatever you about to do.

So if you need a context tied to the lifecycle of the entire application, not just the current Activity call getApplicationContext otherwise getContext is what you want to use

0

but how do I know which context to know because..in the constructor definition it only says ProgressDialog(Context context)?? so I always get confused if to use getApplicationContext or getContext (i.e. MyActivity.this)

0

As I said getContext is what you most commonly will use, so do not bother with getApplicationContext for the moment. You will start to use it once you comprehend the need for it

0

thank you for your reply..i think i got it now :)
could you please tell me a scenario when we'd need a context tied to the lifecycle of the entire application??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.