Skip to main content

Questions tagged [android-context]

Interface to global information about an application environment

android-context
Filter by
Sorted by
Tagged with
2258 votes
32 answers
821k views

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's user avatar
  • 23.4k
1107 votes
21 answers
845k views

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's user avatar
1029 votes
12 answers
278k views

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: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:...
android developer's user avatar
744 votes
30 answers
650k views

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 ...
tyczj's user avatar
  • 73.1k
695 votes
28 answers
419k views

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 ...
gymshoe's user avatar
  • 7,515
655 votes
10 answers
350k views

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. ...
iCurious's user avatar
  • 8,261
502 votes
10 answers
328k views

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 ...
yanchenko's user avatar
  • 57k
467 votes
28 answers
357k views

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 ...
Sako73's user avatar
  • 10.1k
450 votes
4 answers
345k views

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....
mxk's user avatar
  • 43.4k
438 votes
16 answers
484k views

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. ...
ef2011's user avatar
  • 10.6k
404 votes
7 answers
96k views

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 = ...
Alnitak's user avatar
  • 338k
336 votes
18 answers
231k views

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 ...
SapphireSun's user avatar
  • 9,348
280 votes
7 answers
85k views

When to call activity context OR application context?

I do not fully understand the activity context vs the application context. As I understand it so far, each is an instance of its class, which means that some programmers recommend you to use this....
Norfeldt's user avatar
  • 9,220
272 votes
7 answers
162k views

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 ...
t0mm13b's user avatar
  • 34.4k
213 votes
13 answers
171k views

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&...
Stranger B.'s user avatar
  • 9,264
210 votes
6 answers
167k views

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 ...
Lukap's user avatar
  • 31.9k
209 votes
15 answers
181k views

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 ...
Lukap's user avatar
  • 31.9k
203 votes
21 answers
220k views

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 (...
Vincent Williams's user avatar
180 votes
6 answers
187k views

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 ...
Blaskovicz's user avatar
  • 6,130
137 votes
4 answers
65k views

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 ...
Pheonix7's user avatar
  • 2,171
136 votes
1 answer
169k views

get Context in non-Activity class [duplicate]

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's user avatar
  • 1,813
134 votes
12 answers
213k views

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: ...
ssk's user avatar
  • 9,205
108 votes
2 answers
49k views

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's user avatar
  • 1,479
98 votes
1 answer
58k views

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

What is the difference between: getApplicationContext() getBasecontext() getApplication() getParent() Can you elaborate with one simple example?
Nikunj Patel's user avatar
97 votes
9 answers
323k views

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 ...
Ofek Ron's user avatar
  • 8,520
89 votes
3 answers
49k views

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 ...
madu's user avatar
  • 5,370
87 votes
7 answers
77k views

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's user avatar
  • 21.6k
86 votes
4 answers
77k views

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 ...
D4rWiNS's user avatar
  • 2,605
74 votes
12 answers
117k views

How to reference the current or main activity from another class

I often find myself needing to access methods that require referencing some activity. For example, to use getWindowManager, I need to access some Activity. But often my code for using these methods is ...
Johann's user avatar
  • 29.2k
71 votes
5 answers
120k views

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 ...
lorraine batol's user avatar
71 votes
5 answers
33k views

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 ...
Haris Hasan's user avatar
67 votes
9 answers
112k views

How to display AlertDialog in a Fragment?

I want to display an alert dialog in my app. I am using fragments. I tried the below code to do this: AlertDialog ad = new AlertDialog.Builder(context) .create(); ad.setCancelable(...
andro-girl's user avatar
  • 8,099
67 votes
5 answers
88k views

Get context inside onClick(DialogInterface v, int buttonId)?

Getting the context inside onClick(View view), the callback for a button's onClickListener(), is easy: view.getContext() But I can't figure out how to get the context inside onClick(DialogInterface ...
an00b's user avatar
  • 11.4k
66 votes
5 answers
112k views

How to call getResources() from a class which has no context?

In my application I have many classes and activities. Droid is a class which does not have context. Mygame is a class which extends SurfaceView and implements SurfaceHolder.Callback. I am creating an ...
andro-girl's user avatar
  • 8,099
64 votes
3 answers
35k views

Android 13 (SDK 33): PackageManager.getPackageInfo(String, int) deprecated. what is the alternative?

Starting from API level 33 the getPackageInfo(String, int) method of PackageManager class is deprecated. Documentation suggests to use getPackageInfo(String, PackageInfoFlags) instead. But that ...
Marat's user avatar
  • 6,543
62 votes
4 answers
27k views

Android DataBinding where to get context?

I have TextView for showing time. I want to use Android's DataBinding plugin. For formatting time I am using DateUtils.formatDateTime(context, int, int) method which takes Context instance. Is it ...
bakua's user avatar
  • 14.3k
61 votes
4 answers
33k views

SharedPreferences application context vs activity context

I am using several SharedPreferences to store data in my app. Some preferences are used in a lot of activites. I know that the SharedPreferences are internally backed by a map for fast read-access ...
d1rk's user avatar
  • 1,956
59 votes
3 answers
36k views

Android Intent Context Confusing

Can somebody explain this to me please : Intent intent = new Intent(Context, AlarmReceiver.class); I never understood and I seriously think I never will if somebody doesn't try to explain this to me ...
user1880779's user avatar
  • 2,058
58 votes
3 answers
130k views

Android Get Application's 'Home' Data Directory

A simple question, relating to the default 'home' directory when an app writes to the internal memory. By default, any files created are placed by the OS (2.2) in: /data/data/your.package/files When ...
Unpossible's user avatar
  • 10.7k
58 votes
5 answers
100k views

How to use getSystemService in a non-activity class?

I am building an application which triggers an alarm via AlarmManager. I would like to be able to call the Alarm via it's own non-activity class, but since I am not extending Activity, I don't appear ...
eportermd's user avatar
  • 1,761
58 votes
6 answers
64k views

Dagger 2 injecting Android Application Context

I am using Dagger 2 and have it working however I now need access to the Android Application Context. Its not clear to me how to inject and get access to the context. I have tried to do this as ...
user3521637's user avatar
  • 1,642
58 votes
6 answers
61k views

Getting the android context in an adapter

In many of the code samples that I find on the internet the context is obtained in the constructor of an adapter. This context is used to get an inflater to inflate the views in getView method. My ...
Gautam's user avatar
  • 7,898
58 votes
4 answers
39k views

Best practice to pass Context to non-activity classes?

So, my first major application is almost coded and I'm doing optimizations on my code. The app works fine, but I'm not sure about my way of passing the context to other classes. I don't want to do it ...
Iiro Krankka's user avatar
  • 5,139
54 votes
7 answers
54k views

What is different between MainActivity.this vs getApplicationContext()

I am trying ProgressDialog. But I am confused. 1. pd=ProgressDialog.show(MainActivity.this, "", "Fething data"); when I do use (MainActivity.this) then it is ok. But 2. pd=...
AndyError's user avatar
  • 571
52 votes
5 answers
16k views

Is it safe to save the app context to a static variable in Android?

I know that usage of static variables on Android is quite risky, especially if you reference them to activities. However, if I have a class that extends Application (let's call this class "App"), is ...
android developer's user avatar
49 votes
7 answers
62k views

Needing Context in non-Activity classes

I have some classes within my application that need to call Android functions that require the Context as a parameter. I don't have it as the class is not a subclass of the Activity class. What is ...
theblitz's user avatar
  • 6,811
48 votes
2 answers
2k views

Write/create file within Android Instrumentation context

I have a class that creates (or opens) a file to write some data to it. This class receives a Context in the constructor, saves it in an instance field, and then uses it to call the context....
mato's user avatar
  • 1,531
44 votes
3 answers
61k views

Get application context from non activity singleton class

In my android project, I have ImageAdapter class in which I pass app context for some further needs. public class ImageAdapter extends BaseAdapter { private Context c; public ImageAdapter(...
Dmitry's user avatar
  • 680
44 votes
7 answers
182k views

How do I view Android application specific cache?

Is there any way to dynamically view the application specific cache in Android? I'm saving images to the cache (/data/data/my_app_package/cache) and I'm 99% sure they're saving there, but not sure how ...
Karim Varela's user avatar
  • 7,622
43 votes
2 answers
32k views

Difference between getContext() and requireContext() when using fragments

I've been having this doubt since a long time, when I'm working with android fragments and I need to instantiate a Context, or I need to pass a Context as argument for other function, I can choose to ...
Ariel's user avatar
  • 1,099

1
2 3 4 5
40