Why is 'this' passed while creating an instance of Intent? - android

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?

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.

Related

Difference between getApplicationContext() and getActivity()

What is the difference between getApplicationContext() and getActivity() and this in Android?
There is a lot of difference between :
View.getContext():
Returns the context the view is currently running in. Usually the currently active Activity.
Activity.getApplicationContext():
Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
ContextWrapper.getBaseContext():
If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().
Also check :
https://stackoverflow.com/a/10641257/4018207
https://developer.android.com/reference/android/view/View.html#getContext%28%29
There is huge difference. An android application can have more than one activity, when you say getApplicationContext(), it gives you the context of entire application: see details :
However when you say getActivity() it just gives you the instance of activity which you are currently in.

how does startActivity(this, OtherActivity.class) use Context object to start an activity?

I started reading about Context And I am a bit confused.
I found the following definition.
The Context class is an “Interface to global information about an application environment.”
here I am not understanding what android application environment is?
we can use 1. getAssets() 2. getResources() 3. getPackageManager() 4. getString() 5. getSharedPrefsFile() methods ,we can also start an activity ,broadcast an Intent using Context.
I think,The above methods(bolded) provide app's global data to the app,
Are these Resources known as App global data?
So for starting activity ,what global information is used by Android
System?
How does a View use Context (what information it gets from Context)?
I am so much confused about the concept of Context class and how all these things are related.
Please provide any help.
Please go through Context details. I think these things can help you understand the idea of context.
View.getContext():
Returns the context the view is currently running in. Usually the currently active Activity.
Activity.getApplicationContext():
Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
ContextWrapper.getBaseContext():
If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().

What is the use of Context to start an activity in Android?

When starting another activity, the method startActivity(someintent) is used, that intent, at the same time, contains the parameters this for context and a class object.
What is the use of the this parameter, given that the context is already known since the activity itself is a subclass of context? And, given that I create an intent as:
Intent myIntent = new Intent(this,someclass.class);
Am I only able to use the method startActivity when inside this context specified when creating myIntent. In other words, I can't use startActivity with the same intent from another activity.
Several reasons:
You are not required to use this as the context when creating a new Intent in an activity. (You might, for instance, create a ContextThemeWrapper to apply a separate theme.)
The Intent constructor can be called from outside an Activity. In any event, the Intent constructor has no way of knowing what object is calling the constructor (if any—it could even be called from a static context).
There are other uses for an Intent besides calling startActivity().

How do I find out whether the Context object is being Leaked from one activity to another?

I want to figure out whether the Context object of one activity is being leaked to another activity. Does the following code leak the context of one activity to another?
Intent intent = new Intent(context, Demo.class);
context.startActivity(intent);
No, that doesn't "leak" your context. In general, to avoid leaking an Activity context, here's a few tips to follow:
Never hold a static reference to a Context
Do not pass Views between Activitys
Whenever you don't need a Context with an Activity reference, use the application's Context (context.getApplicationContext()).
Use WeakReference when holding onto a Context while running some background operation (or whenever feasible really)
As an addendum, use the Memory Analysis Tool (MAT) for eclipse to inspect a heap dump.

What context to use when starting a new activity on Android?

In an Android app when I need to start a new activity, what's the difference on using the current activity context X application context?
The context of an application is the state your phone is in when you are within an application.
You use this context to refer to the application's UI elements and resources.
When you switch context, the application follows the Android application lifecycle.
You use the current context to start a new activity within an application.
Like this:
Intent i = new Intent(activity, com.MainApp.ActivityToStart);
currentActivity.startActivity(i);
Hope this helps.
Application context has information about whole application life cycle while activity context has information about a particular activity. You should use activity context instead of Application context because it is the recommended way. Read this for more details
Android documentation says
public Context getApplicationContext ()
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.
Activity extends Context, so you can use this. It doesn't matter for the usage of the activity, but if you initialize objects that will remain after the activity is used, you should use the application context.

Categories

Resources