Questions tagged [android-context]

Interface to global information about an application environment

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Context - Android Developer

1980 questions
208
votes
15 answers

getActivity() returns null in Fragment function

I have a fragment (F1) with a public method like this public void asd() { if (getActivity() == null) { Log.d("yes","it is null"); } } and yes when I call it (from the Activity), it is null... FragmentTransaction transaction1 =…
Lukap
  • 31,523
  • 64
  • 157
  • 244
207
votes
13 answers

How to get a context in a recycler view adapter

I'm trying to use picasso library to be able to load url to imageView, but I'm not able to get the context to use the picasso library correctly. public class FeedAdapter extends RecyclerView.Adapter { private List
Stranger B.
  • 9,004
  • 21
  • 71
  • 108
189
votes
20 answers

How to get Context in Android MVVM ViewModel

I am trying to implement MVVM pattern in my android app. I have read that ViewModels should contain no android specific code (to make testing easier), however I need to use context for various things (getting resources from xml, initializing…
Vincent Williams
  • 2,836
  • 3
  • 17
  • 22
180
votes
6 answers

How do you obtain a Drawable object from a resource id in android package?

I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* package? for example if drawableId was…
Blaskovicz
  • 6,122
  • 7
  • 41
  • 50
135
votes
1 answer

get Context in non-Activity class

In an android Application, is there any way to get the context in android in a non-activity class if the activity class name is known?
Developer
  • 1,803
  • 2
  • 15
  • 26
135
votes
4 answers

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

I'm new to android and I'm trying to understand the difference between getApplication(), getApplicationContext(), getBaseContext(), getContext() and someClass.this and especially when to use the these methods in the following code lines: When I…
Pheonix7
  • 2,131
  • 5
  • 21
  • 38
132
votes
12 answers

Using getResources() in non-activity class

I am trying to use getResources method in a non-activity class. How do I get the reference to the "resources" object so that I can access the xml file stored under resources folder? Example: XmlPullParser xpp =…
ssk
  • 9,045
  • 26
  • 96
  • 169
107
votes
2 answers

What is the difference between Activity and Context?

Are Activity and Context the same, or are there differences? When should I have a method pass an Activity, and when a Context?
Android
  • 1,469
  • 3
  • 13
  • 21
98
votes
2 answers

getApplicationContext(), getBaseContext(), getApplication(), getParent()

What is the difference between: getApplicationContext() getBasecontext() getApplication() getParent() Can you elaborate with one simple example?
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
92
votes
9 answers

How to get my activity context?

I don't really get the idea behind how this whole thing works really, so if I have some class A that need the context of a class B which extends Activity, how do i get that context? I'm searching for a more efficient way than giving the context as a…
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
87
votes
7 answers

Close the current activity when you only have a reference to Context

If I have a reference to Context, is it possible to finish the current activity? I don't have the reference to current activity.
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
86
votes
3 answers

Android - Getting context from a Broadcast receiver onReceive() to send to

I basically want to make an intent and pass it to a service from my BroadcastReceiver's onReceive(). So far I always used View.getContext(), but here, I'm stuck. How exactly can I get the context so I can use public Intent (Context packageContext,…
madu
  • 5,232
  • 14
  • 56
  • 96
86
votes
4 answers

getting context in AsyncTask

I am trying to get the context in my AsyncTask of the class called Opciones(this class is the only one that call that task) but I don't know how to do it, I saw some code like this: protected void onPostExecute(Long result) { …
D4rWiNS
  • 2,585
  • 5
  • 32
  • 54
70
votes
5 answers

Is it possible to get application's context in an Android Library Project?

I would like to get the context of application which has reference/hosted my library at run-time inside one class of my library project. Is it possible? If yes, how? Thanks Update I don't want my user to pass context in parameter to my library…
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
70
votes
5 answers

android - How to get view from context?

I want to get the view or findViewById() from Context? Or from intent? I'm trying to reach a specific view in my broadcast receiver and the parameter of onReceive are context and intent. Well, I have a class and within it is my broadcast receiver. …
lorraine batol
  • 6,001
  • 16
  • 55
  • 114