Junior
Why is context needed in Android and what are its main functions?
sobes.tech AI
Answer from AI
Context in Android provides access to global information about the application and its environment, as well as resources. It acts as a bridge between UI/business logic and system services.
Main functions:
- Access to resources: String resources, images, styles, themes, etc.
- Launching activities and services: Methods
startActivity(),startService(). - Access to system services: LayoutInflater, UserManager, sensors, etc.
- Working with the file system: Access to cache directories, application files.
- Getting information about the application package: Name, version, signature.
- Working with SharedPreferences.
Different types of context (Application Context, Activity Context) have different scopes and lifecycles, which affects where and how they can be safely used. Application Context lives as long as the application, Activity Context - as long as the activity exists. Using Activity Context in long-lived objects can lead to memory leaks.