Reputation: 3625
The main point of this question is to get clarification of when it is necessary to use 'this' or getApplicationContext() or some other form of getting Context. Ex.
Settings.System.putInt(getContentResolver(), Settings.System
.SCREEN_BRIGHTNESS_MODE, 0);
vs.
Settings.System.putInt(getApplicationContext().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
Is there a difference of how these two function?
Upvotes: 2
Views: 358
Reputation: 16268
The question "what the Context is" is one of the most difficult questions in Android universe.
Context defines methods that access system resources, retrieve application's static assets, check permissions, perform UI manipulations and many more. In essence, Context
is an example of God Object anti-pattern in production.
When it comes to which kind of Context
should we use, it becomes very complicated because except for being God Object, the hierarchy tree of Context
subclasses violates Liskov Substitution Principle brutally.
This blog post attempts to summarize Context
classes applicability in different situations.
Let me copy the main table from that post for completeness:
+----------------------------+-------------+----------+---------+-----------------+-------------------+ | | Application | Activity | Service | ContentProvider | BroadcastReceiver | +----------------------------+-------------+----------+---------+-----------------+-------------------+ | Show a Dialog | NO | YES | NO | NO | NO | | Start an Activity | NO¹ | YES | NO¹ | NO¹ | NO¹ | | Layout Inflation | NO² | YES | NO² | NO² | NO² | | Start a Service | YES | YES | YES | YES | YES | | Bind to a Service | YES | YES | YES | YES | NO | | Send a Broadcast | YES | YES | YES | YES | YES | | Register BroadcastReceiver | YES | YES | YES | YES | NO³ | | Load Resource Values | YES | YES | YES | YES | YES | +----------------------------+-------------+----------+---------+-----------------+-------------------+
- An application CAN start an Activity from here, but it requires that a new task be created. This may fit specific use cases, but can create non-standard back stack behaviors in your application and is generally not recommended or considered good practice.
- This is legal, but inflation will be done with the default theme for the system on which you are running, not what’s defined in your application.
- Allowed if the receiver is null, which is used for obtaining the current value of a sticky broadcast, on Android 4.2 and above.
As for the getContentResolver()
call - activity.getContentResolver()
and application.getContentResolver()
are equivalent (Activity
delegates this call to Application
under the hood).
Upvotes: 1
Reputation: 2826
In the Android system, a context can be an Activity, Service or Application. When you call context.getApplicationContext();
from any context, you get the context of the Application class which you define in Manifest. Since the Application class maintains a global state of the app, the application context is available at any time when the app is running (in the foreground or background).
when it is necessary to use 'this' or getApplicationContext()
When you need the context for an action which is bound and runs inside your Activity or Service, it's always good to use this
. On the other hand, when you perform something that is more app specific and isn't bound or dependent to the calling Activity/Service, you can use getApplicationContext();
In your case, you're using your context just to get some context resource, so it's the same in both cases. The difference occurs, when you pass the context to another class knowing that the class will use the context from time to time.
Upvotes: 0