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
2241
votes
32 answers

What is 'Context' on Android?

In Android programming, what exactly is a Context class and what is it used for? I read about it on the developer site, but I am unable to understand it clearly.
Brigadier
  • 23,191
  • 4
  • 18
  • 9
1097
votes
21 answers

Static way to get 'Context' in Android?

Is there a way to get the current Context instance inside a static method? I'm looking for that way because I hate saving the 'Context' instance each time it changes.
Andrea Baccega
  • 27,211
  • 13
  • 45
  • 46
1026
votes
12 answers

What's "tools:context" in Android layout files?

Starting with a recent new version of ADT, I've noticed this new attribute on the layout XML files, for example:
android developer
  • 114,585
  • 152
  • 739
  • 1,270
738
votes
31 answers

Using context in a fragment

How can I get the context in a fragment? I need to use my database whose constructor takes in the context, but getApplicationContext() and FragmentClass.this don't work so what can I do? Database constructor public Database(Context ctx) { …
tyczj
  • 71,600
  • 54
  • 194
  • 296
694
votes
28 answers

Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

My Activity is trying to create an AlertDialog which requires a Context as a parameter. This works as expected if I use: AlertDialog.Builder builder = new AlertDialog.Builder(this); However, I am leery of using "this" as a context due to the…
gymshoe
  • 7,495
  • 5
  • 20
  • 21
648
votes
10 answers

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

What is the difference between getContext() , getApplicationContext() , getBaseContext() , and "this"? Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible.
iCurious
  • 8,161
  • 7
  • 28
  • 46
497
votes
10 answers

Using Application context everywhere?

In an Android app, is there anything wrong with the following approach: public class MyApp extends android.app.Application { private static MyApp instance; public MyApp() { instance = this; } public static Context…
yanchenko
  • 56,576
  • 33
  • 147
  • 165
458
votes
28 answers

Calling startActivity() from outside of an Activity context

I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign an OnClickListener. In the onClick method of the…
Sako73
  • 9,957
  • 13
  • 57
  • 75
449
votes
4 answers

getApplication() vs. getApplicationContext()

I couldn't find a satisfying answer to this, so here we go: what's the deal with Activity/Service.getApplication() and Context.getApplicationContext()? In our application, both return the same object. In an ActivityTestCase however, mocking the…
mxk
  • 43,056
  • 28
  • 105
  • 132
430
votes
15 answers

How to get package name from anywhere?

I am aware of the availability of Context.getApplicationContext() and View.getContext(), through which I can actually call Context.getPackageName() to retrieve the package name of an application. They work if I call from a method to which a View or…
ef2011
  • 10,431
  • 12
  • 49
  • 67
402
votes
8 answers

What's the difference between the various methods to get an Android Context?

In various bits of Android code I've seen: public class MyActivity extends Activity { public void method() { mContext = this; // since Activity extends Context mContext = getApplicationContext(); mContext =…
Alnitak
  • 334,560
  • 70
  • 407
  • 495
331
votes
17 answers

getString Outside of a Context or Activity

I've found the R.string pretty awesome for keeping hardcoded strings out of my code, and I'd like to keep using it in a utility class that works with models in my application to generate output. For instance, in this case I am generating an email…
SapphireSun
  • 9,170
  • 11
  • 46
  • 59
279
votes
7 answers

When to call activity context OR application context?

There has been a lot of posting about what these two contexts are.. But I'm still not getting it quite right As I understand it so far: Each is an instance of its class which means that some programmers recommend you to use…
Norfeldt
  • 8,272
  • 23
  • 96
  • 152
270
votes
7 answers

Difference between Activity Context and Application Context

This has me stumped, I was using this in Android 2.1-r8 SDK: ProgressDialog.show(getApplicationContext(), ....); and also in Toast t = Toast.makeText(getApplicationContext(),....); using getApplicationContext() crashes both ProgressDialog and…
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
210
votes
6 answers

Call getLayoutInflater() in places not in activity

What does need to be imported or how can I call the Layout inflater in places other than activity? public static void method(Context context){ //this doesn't work the getLayoutInflater method could not be found LayoutInflater inflater =…
Lukap
  • 31,523
  • 64
  • 157
  • 244
1
2 3
99 100