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
69
votes
12 answers

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 in some other class that has no reference to an…
Johann
  • 27,536
  • 39
  • 165
  • 279
67
votes
9 answers

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(false); ad.setTitle(title); …
andro-girl
  • 7,989
  • 22
  • 71
  • 94
66
votes
5 answers

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 object of Droid in mygame class and setting the…
andro-girl
  • 7,989
  • 22
  • 71
  • 94
66
votes
4 answers

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 v, int buttonId), the callback for a dialog's…
an00b
  • 11,338
  • 13
  • 64
  • 101
61
votes
4 answers

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 and written to sdcard when settings are changed. I…
d1rk
  • 1,956
  • 2
  • 14
  • 19
60
votes
4 answers

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 possible to get context include element? Or do I…
bakua
  • 13,704
  • 7
  • 43
  • 62
58
votes
4 answers

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 the wrong way. I stumbled upon articles and…
Iiro Krankka
  • 4,959
  • 5
  • 38
  • 42
58
votes
3 answers

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 reading in files, the same default is used, when…
Unpossible
  • 10,607
  • 22
  • 75
  • 113
58
votes
5 answers

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 to have any 'context'. This concept confuses me,…
eportermd
  • 1,751
  • 4
  • 13
  • 8
58
votes
3 answers

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 in depth. This whole context thing is so…
user1880779
  • 2,038
  • 8
  • 30
  • 40
58
votes
6 answers

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 Question is why bother getting the context in the…
Gautam
  • 7,868
  • 12
  • 64
  • 105
56
votes
6 answers

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 follows: @Module public class MainActivityModule { …
user3521637
  • 1,622
  • 2
  • 18
  • 25
55
votes
2 answers

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 function is only available from API level 33. My…
Marat
  • 6,142
  • 6
  • 39
  • 67
53
votes
7 answers

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=ProgressDialog.show(getApplicationContext(), "", "Fething data"); When I do…
AndyError
  • 561
  • 1
  • 6
  • 10
52
votes
5 answers

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 it safe to reference to the instance of this…
android developer
  • 114,585
  • 152
  • 739
  • 1,270