IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

difference entre this et getApplicationContext


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    542
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 542
    Par défaut difference entre this et getApplicationContext
    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.

  2. #2
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour,

    Car j'ai remarqué pour les AlertDialog, le mot this comme context fonctionne mais pas getApplicationContext()
    Tu trouveras à ce lien une explication sur ce sujet http://android-developers.blogspot.c...ory-leaks.html

    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
    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.

    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()

Discussions similaires

  1. Différences entre Delphi et Visual Basic ?
    Par Anonymous dans le forum Débats sur le développement - Le Best Of
    Réponses: 75
    Dernier message: 30/03/2009, 20h09
  2. La difference entre XSL et XSLT?
    Par pantin dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 27/06/2003, 15h14
  3. Difference entre fenetre et boite de dialog
    Par billyboy dans le forum Windows
    Réponses: 2
    Dernier message: 02/06/2003, 15h43
  4. [] Difference entre MSHFlexGrid et MSFlexGrid
    Par olivierx dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 23/04/2003, 08h48
  5. Difference entre types d'Adresse IP
    Par freud dans le forum Développement
    Réponses: 3
    Dernier message: 02/03/2003, 02h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo