Junior — Middle
Can you list the types of contexts and their main features?
sobes.tech AI
Answer from AI
In Android, there are several types of Context, each with its own characteristics and use cases:
-
Application Context
- Lives as long as the application is running.
- Used for operations not related to UI, such as resource access, starting services.
- Not tied to a specific Activity or View.
-
Activity Context
- Tied to the lifecycle of an Activity.
- Used for creating UI, launching dialogs, inflating layouts.
- Can lead to memory leaks if held longer than the Activity's lifecycle.
-
Service Context
- Context associated with a Service.
- Used within services to access resources and system functions.
-
ContextWrapper
- Wraps another Context, allowing modification or extension of behavior.
The main rule is to use Application Context when a context is needed that lives longer than an Activity, and Activity Context when interaction with UI or components tied to the Activity is required.