i. If the control or variable you are creating should belong to application level then use applicationContext.
ii. If the control or variable you are creating should belong to Activity level then use this pointer.
iii. If this is not available, still we can get activity context by specifying ActivityName.this
Description:
There are two types of contexts available in android to create any component.
1. this context (or) this pointer
2. application context
When programmer wants to create any component or control, then you have to use one of the contexts.
eg: TextView t = new TextView(this);
Here we are using this pointer (context).
eg: static TextView st = new TextView(getApplicationContext());
Here we are using application context, because it is a static variable whose life time will be through out application life time.
If you use this pointer here, then it will leak memory (memory wastage) of your activity.
When to use this ; getapplicationcontext:
1.If the control or variable you are creating should belong to application level then use applicationContext.
2.If the control or variable you are creating should belong to Activity level then use this pointer or this context.
Note: Generally people will think that java will not have memory leaks. But if you don't use contexts properly, then it might lead to some dangerous memory leaks in your android application.
Tip : Don't ever link between this poin
ii. If the control or variable you are creating should belong to Activity level then use this pointer.
iii. If this is not available, still we can get activity context by specifying ActivityName.this
Description:
There are two types of contexts available in android to create any component.
1. this context (or) this pointer
2. application context
When programmer wants to create any component or control, then you have to use one of the contexts.
eg: TextView t = new TextView(this);
Here we are using this pointer (context).
eg: static TextView st = new TextView(getApplicationContext());
Here we are using application context, because it is a static variable whose life time will be through out application life time.
If you use this pointer here, then it will leak memory (memory wastage) of your activity.
When to use this ; getapplicationcontext:
1.If the control or variable you are creating should belong to application level then use applicationContext.
2.If the control or variable you are creating should belong to Activity level then use this pointer or this context.
Note: Generally people will think that java will not have memory leaks. But if you don't use contexts properly, then it might lead to some dangerous memory leaks in your android application.
Tip : Don't ever link between this poin
No comments:
Post a Comment