Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android development, the term Context
is fundamental and is used extensively. It's a reference to the state of the app, and provides resources, themes, and other pieces of environment information. Essentially, it's a bridge to the underlying Android system.
Context
:Context
.Context
.Context
.LAYOUT_INFLATER_SERVICE
, NOTIFICATION_SERVICE
, and others, you need a Context
to get hold of them.Context
:Application Context: This is an instance of the entire application life cycle and is independent of the activity life cycle. It's suitable for operations that aren't associated with a UI, like setting up singletons, long-lived listeners, or services.
Usage:
getApplicationContext();
Activity Context: Each Activity
in an app has its own context, which is a subclass of Context
. This context is tied to the lifecycle of that activity. It is also often used to load UI resources and start new activities.
Usage:
MyActivity.this // or simply: this // when inside an activity
Service Context: Like Activity
, a Service
in Android also extends Context
, so it has its own context. It's tied to the lifecycle of that service.
Broadcast Receiver Context: A BroadcastReceiver
also receives a Context
when its onReceive
method is called. This context is only valid for the duration of onReceive
execution.
View Context: Every View
in Android also has a reference to the Context
that was used to create it.
Memory Leaks: One of the common pitfalls in Android is leaking a Context
. This can happen when you keep a long-lived reference to an Activity
or a View
. This is why, for long-lived objects, it's usually better to use the ApplicationContext
which is tied to the application lifecycle and won't cause individual activities to be retained.
Styling and Theming: Be cautious about using the ApplicationContext
to inflate views or access UI-related resources. The application context doesn't carry the theme of the current activity. Hence, inflating views using the application context can lead to styling issues.
In summary, the Context
in Android is a powerful and versatile concept, but it's essential to understand its different types and their lifecycles to use it effectively and avoid potential issues.
Using Context in Android Programming:
Context
object.String appName = context.getString(R.string.app_name);
Context and Resource Access in Android:
Context
object.int drawableId = context.getResources().getDrawable(R.drawable.icon);
Context and Service Binding in Android:
Context
to bind to services, allowing communication between components.Intent intent = new Intent(context, MyService.class); context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);