Bonjour,
Je voudrais connaitre la différence entre le mot clé this utilisé comme Context et la méthode getApplicationContext()?
Car j'ai remarqué pour les AlertDialog, le mot this comme context fonctionne mais pas getApplicationContext().
Merci.
Bonjour,
Je voudrais connaitre la différence entre le mot clé this utilisé comme Context et la méthode getApplicationContext()?
Car j'ai remarqué pour les AlertDialog, le mot this comme context fonctionne mais pas getApplicationContext().
Merci.
Bonjour,
Tu trouveras à ce lien une explication sur ce sujet http://android-developers.blogspot.c...ory-leaks.htmlCar j'ai remarqué pour les AlertDialog, le mot this comme context fonctionne mais pas getApplicationContext()
GetApplicationContext te retourne le context associé à l'application.Ceci est utile pour les objets ayant une longue vie par rapport au context de l'activity.This context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication().
In summary, to avoid context-related memory leaks, remember the following:
Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
Try using the context-application instead of a context-activity
Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance
A garbage collector is not an insurance against memory leaks
http://developer.android.com/referen...ationContext()
public abstract Context getApplicationContext ()
Since: API Level 1
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
Consider for example how this interacts with registerReceiver(BroadcastReceiver, IntentFilter):
If used from an Activity context, the receiver is being registered within that activity. This means that you are expected to unregister before the activity is done being destroyed; in fact if you do not do so, the framework will clean up your leaked registration as it removes the activity and log an error. Thus, if you use the Activity context to register a receiver that is static (global to the process, not associated with an Activity instance) then that registration will be removed on you at whatever point the activity you used is destroyed.
If used from the Context returned here, the receiver is being registered with the global state associated with your application. Thus it will never be unregistered for you. This is necessary if the receiver is associated with static data, not a particular component. However using the ApplicationContext elsewhere can easily lead to serious leaks if you forget to unregister, unbind, etc.
Juste une précision : Utilise au maximum le context de l'activity au lieu de GetBaseContext()
Partager