[Android] Context of use

Android developers often need to start using Context Activity, or open SharedPreferences, or build a Dialog. Recent always used getContext (), getApplicationContext (), this, etc., to get the Context, so write this to clarify ideas. Context determines that you need time to find out which function is used.

Context

Context What is it?

Personal understanding is: Context is the context, in other words, running environment. It can be used to create new objects, access to resources.

The official reference documentation, only one paragraph:

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

So some say three things (simple translation follows):

  1. It is to obtain environmental information application interfaces.
  2. It is an abstract class, implemented by Andrews.
  3. It allows access to application-specific resources and the like; execute application-level operations, such as opening a activity.

Context acquisition methods

The method of obtaining Context are summarized as follows:

  1. Activity class using this keyword
  2. SomeActivity.this
  3. In Fragment can getActivity ()
  4. getContext()
  5. getApplicationContext()
  6. getBaseContext()

So get these types of Context object the way what similarities and differences?

First, these methods are classified, classified according to the place of use:

Activity can be used in:

  1. Activity class using this keyword
  2. SomeActivity.this
  3. getApplicationContext()
  4. getBaseContext()

In Fragment can be used:

  1. getContext()
  2. getActivity()

In the View can be used:

  1. getContext()

For their meanings

Activity class using this keyword, the equivalent SomeActivity.this.

As long getApplicationContext () Gets Context entire application, obtain the survival period of the object and application.

getContext () View Activity acquired is currently active, Fragment returns associated Context

getActivity () Returns the Activity associated with the current Fragment

getBaseContext () Gets ContextWrapper original context

Whether Application, or Activity, they have inherited Context.

Avoid memory leaks caused by Context

Memory leak caused by the Context consists of two main reasons:

  1. The Context object references survival time longer than the incoming activity.
  2. Members of inner classes and anonymous inner classes implicitly holding the object outside the class lead.

The first case is easy to understand, when activity destroyed, as well as object reference to this activity, then the GC will not recover this activity.
The reason the second case, see link 4. The main target is talking about inner classes implicitly held outside of class GC does not lead to recovery of this activity. Local memory leak may occur in Andrews is an activity which has run a Runnable's not over, this activity has been destroyed. But this activity is Runnabl implicitly held, resulting in activity can not be recovered.

That being the case, then just do not use this good! With getApplicationContext () does not like it?

However, getApplicationContext () has its drawbacks!

Link 3 explains why no getApplicationContext (): it is not a complete Context. It may lead to some GUI related issues. For example AlertDialog.Builder not use getApplicationContext (), because the dialog needs some topics related to information, and Application does not contain such information. (You need to use a Theme.AppCompat theme (or descendant) with this activity)

So when using getApplicationContext (), when to use this?
Generally, if the context does not need ui related operations, use getApplicationContext (). If the object survival time may be longer than activity, consider using getApplicationContext (). Otherwise to ensure the destruction of the former activity, dereference activity, with this just fine.

Reference links

  1. https://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and#
  2. https://juejin.im/post/58cb97e1128fe1006c84aafe
  3. https://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context
  4. https://blog.csdn.net/leunging/article/details/53080863

Guess you like

Origin www.cnblogs.com/zhouzekai/p/11093891.html