App Components
App components are the essential building blocks of an Android app.
Each component is an entry point through which the user can enter your app.
There are four main components, Each type serves a distinct purpose and has a distinct lifecycle that defines how a component is created and destroyed.
Context
Context is an object that provides information about the current state of the application and facilitates access to application resources and system-level operations.
Using a Context, you can:
Access resources (strings, layouts, drawables)
Start activities and services
Get system services (LayoutInflater, NotificationManager, etc.)
Access application assets
Show toasts, dialogs, notifications
Access preferences, databases, file system
For example,
Accessing Resources
Accessing System Services
Accessing App Components
Types of Context
1. Application Context
Lifetime: entire app process
Tied to:
ApplicationUse when you need something that should live as long as the app
Examples:
Starting a Service
Accessing shared preferences
Long-lived objects (singletons, repositories)
2. Activity Context
Lifetime: as long as the Activity
Tied to: UI and theme
Required for anything that touches the screen
Examples:
Inflating layouts
Showing dialogs
Starting another Activity
Use this for UI. Period.
3. Service Context
Lifetime: as long as the Service
Similar to Application Context, but scoped to the service
Used when:
You’re inside a Service and need context-aware operations
Mistakes when using the Context
Using Application Context for UI stuff Enjoy your crashes and un-themed dialogs.
Holding Activity Context in static variables Congrats, you invented a memory leak.