1

I am new to android app development. I am trying to understand what is intent and its uses.

My question is that while starting another activity, why is 'this' keyword passed as the context parameter for the intent?

Intent foo = new Intent(this, viewContacts.class);

I understand that the any activity extends Context class, but why is it that we are passing the activity context and not the application context?

My Point-

When another activity starts the current activity will get destroyed but its context will be passed to the other one. Referring to this article, it says that

The most obvious way of avoiding context related memory leak is to avoid escaping the context outside of its own scope.

So aren't we passing the context of current activity to another one where the first one goes out of scope? Isn't it an example of memory leak?

Rohan Bhatia
  • 1,870
  • 2
  • 15
  • 31
  • 1
    In this case (the 2-argument constructor for Intent), the `Context` parameter is only used to determine the package name of the target `Activity`. That's it. Please see my answer to a similar question here: http://stackoverflow.com/a/38870648/769265 – David Wasser Dec 26 '16 at 23:23
  • @DavidWasser thanks for your reply! – Rohan Bhatia Jun 26 '17 at 17:22

1 Answers1

3

why is it that we are passing the activity context and not the application context?

Either would work here. this is less typing and faster to execute than is getApplicationContext().

When another activity starts the current activity will get destroyed but its context will passed to the other one.

You are assuming that the Intent holds onto this Context. It does not.

So aren't we passing the context of current activity to another one where the first one goes out of scope?

No.

An Intent can either be implicit or explicit. An explicit Intent is one that has a ComponentName attached, identifying the specific app (by package name) and Java class (by fully-qualified class name) of the component for which this Intent is intended. The two-parameter constructor, providing the Context and Class object, is used to build that ComponentName. Neither the Intent nor the ComponentName hold onto the Context after the constructor work is completed.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks! That answers my question. But you just said that either would work here. I am really confused as Application Context and Activity Context have different Information. – Rohan Bhatia Dec 26 '16 at 17:48
  • So how come both of them can be used here?Also, why is 'this' faster to execute than getApplicationContext() ? Is it because when we call getApplicationContext() while 'this' is already available to us. – Rohan Bhatia Dec 26 '16 at 17:51
  • 1
    @RohanBhatia: I recommend [this blog post](https://possiblemobile.com/2013/06/context/) to give you an idea of the different roles of different `Context` objects. Both can be used because they both will return the same values for the relevant methods (e.g., `getPackageName()`). `this` is an object; `getApplicationContext()` is a method that returns an object. Doing work (calling a method) is always slower than not doing that work. – CommonsWare Dec 26 '16 at 17:52