What context to use when starting a new activity on Android? - 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.

Related

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

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.

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

Context Methods in Android

Hello I am noob in android. I see that in android you can get context using different method. I could not understand the difference between them and when to use what .
Methods : getApplicationContext(), getContext(), getBaseContext(), this (Activity)
An 'Application context' is associated with the Application and will always be the same throughout the life cycle of your app (getApplicationContext())
The 'Activity context' is associated with the activity and could possibly be destroyed many times as the activity is destroyed during screen orientation changes and such.(getContext())
Generally don't use getBaseContext(), rather use one of the previous ones as needed.
You might want to use the Application Context (Activity.getApplicationContext()) rather than using the Activity context (this). This is because 'this' needs to be called from within an Activity. (Activity extends Context )

getting context in java android

hey i have an android application in which i monitor gmail server in a thread in background..this thread runs infinitely ...on getting a mail in the mailbox I have to make a toast..but i am not able to get the context of current activity(since there are multiple activities in the app)...how to get context of current running activity or may be application..getApplicationcontext gives null.
If you're running an Activity or a Service, both are Context themselves, so calling this suffice. On any callback method for a listener, this will refer to the listener, not to the Activity. You can refer to the Context using any View:
Context context = myView.getContext();
In toasts and listeners and everywhere, if you need to get resources, You could use
Resources.getSystem().getString(android.R.string.cancel)
It has absolutely universal placement, but supports only system resources.
Use this or getApplicationContext() to get the current context.
For example,
Context mContext=this;
or
Context mContext=getApplicationContext();
If the actvity is any child Acivity of some tabHost, then try
getParent().getApplicationContext() also.
You can get the context of the Activity by calling on the Class name. For instance, if you Activity is called from inside MainActivity you can get the context by typing:
Context context = this;

Categories

Resources