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;
Related
I'm badly confused and hope to get your help understanding this concept.
I have an app with 3 activities, splash, login and main and the main activity is a multi fragment drawer activity that uses sqlite.
In my fragments i need to use context many places. I used to have a static context in my main activity defined and passed that around but in another questions someone suggested not to use static context to avoid leaks and i took the advice.
I had to change a few things and got things working. I use getapplicationcontext() but now my application now consistently crashes. The crashes are more prominent when the app is put in background.
My question is, which activity's context should i be using to start with? Splash? Login or main? How would you get access to the context in resume?
Thanks
If you are in A fragment you can use getActivity() to access its container activity context.
If you are try to access activity context from an adapter class or dialog, you must pass the activity context to the constructor of the adapter class or dialog
If you are in an activity you can use this or yourActivity.this as per the situation.
For example
1)If you want to access activity context from onResume() or onCreate() you can simply use this.
2)If you are try to access activity context from an inner class like retrofit call then you must use yourActivity.this for activiy context
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.
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().
I read this discussion but still have a question.
If I use getDefaultSharedPreferences(getApplicationContext()) and getDefaultSharedPreferences(SomeActivity.this), does it gurantee to give same result (same xml preference file access)?
yes it does. The context parameter is used to get the package name, that will be used as name for the xml file in which android stores your values. You can see the androis's source code here
Wishing you a happy new year-2014
Both will behave same !!
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().
Please Note that behind the scenes
getDefaultSharedPreferences(context)
calls
getSharedPreferences(context.getPackageName(), MODE_PRIVATE)
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;
Hope now you understand the things so we can say that they will produce same behave.
Please refer http://developer.android.com/reference/android/app/Activity.html
and
http://developer.android.com/reference/android/content/Context.html
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.