107

Are Activity and Context the same, or are there differences?

When should I have a method pass an Activity, and when a Context?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Android
  • 1,469
  • 3
  • 13
  • 21
  • @Egor Thnx man,and surly i ll take care for it.. before asking question.. can u help me in one more question.. what is the root of all activity in android – Android Jun 29 '11 at 10:07
  • please check the discussion below to clear the question – Egor Jun 29 '11 at 10:21
  • Too bad none of these answers are very complete. This is the first item that pops up for a google search. – SMBiggs May 25 '16 at 05:32

2 Answers2

135

As far as I understand: Context is the Base Object. So every Activity same as Application derives from Context. This means that every Activity and every Application IS a Context;

From developer.android.com Activity

java.lang.Object
  ↳ android.content.Context
      ↳ android.content.ContextWrapper
          ↳ android.view.ContextThemeWrapper
              ↳ android.app.Activity

And Application

java.lang.Object
↳   android.content.Context
   ↳    android.content.ContextWrapper
       ↳    android.app.Application

An Application context lasts, as long as your app is alive, while the Activity context dies with your Activity (it is not valid after onDestroy of that Activity).

So if you need the Context across Activities (i.e. in a Singleton) you will be better off using an Application context.

Usually on Android Framework methods where a context is expected, it makes no difference which one you pass. But be always aware of MemoryLeaks if you're keeping long-living References to a Context

André Herculano
  • 1,258
  • 20
  • 33
Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • 2
    Yes, I'm sorry, I've checked the link, indeed Context is a superclass of Activity. Anyways, it's not completely right to say that Activity is a Context, Activity is a more complex object, while Context is just a block that holds the information and gives the access to resources. – Egor Jun 29 '11 at 10:20
  • @Rafael T Thnx for giving proper answer – Android Jun 29 '11 at 10:50
  • 18
    it is completely right if I refer to Object-Orientated languages: every Object in Java derives from Object.class, so in FACT everything IS an Object. To be more specific: Acivity is a specialization of Context. If I have an Abstract Class Animal and two Classes which are deriving from Animal (Cat, Dog), these two are at least Animals. Everywhere an Animal is required you're fine to pass Cats or Dogs, as it is the same for Context! Please confirm Informations before you downvote me – Rafael T Jun 29 '11 at 11:07
  • 1
    _"An Activity context lasts, as long as your app is alive, while the Activity context dies with your Activity"_ seems off. Shouldn't the first be _Application_ context? – FirstOne May 08 '18 at 19:05
10

As You can see on the Android doc:

The Activity class extends from "ContextThemeWrapper", and this one from "ContextWrapper", and that one from "Context".

So, yes, An Activity extends the Context!

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
zapotec
  • 2,628
  • 4
  • 31
  • 53