Android 개발을 하면서 심심치않게 쓰는 Context 에 대한 존재를 알아보려고 합니다.
Context 정의
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system.
- Android Developer Doc
한마디로 애플리케이션에 대해 시스템이 관리하고 있는 정보에 접근하거나 안드로이드 시스템에서 제공하는 API 를 사용할수 있는 기능을 가진 인터페이스
Context 의 역할❓
🔆안드로이드는 어플리케이션과 프로세스가 별도로 관리합니다!!
- 프로세스 : OS에 의해 관리
- 애플리케이션 : ActivityManagerService 에 의해 관리
즉, 애플리케이션에 관련 정보를 가진것은 ActivityManagerService
한마디로 Context 는 ActivityManagerService 에 접근하기 위한 중간다리 역할!
ActivityMaangerService 의 역할
- 통합 문의 채널 역할
- 여러앱을 통합해서 관리하는구조
- 자신에게 온 요청이 어느요청인지 파악해야함
Context 의 종류
Activiy Context❓
- Activiy 에서 유일!
- 싱글톤이고 앱이 살이있는동안 변경 X
- Activity 가 파괴될때 함께 파괴됨
Application Context❓
- Application 에서 유일!
- Application Context 안에 Acitivy Context 가 존재(Activity 는 Application에 종속되기 때문에)
단, Application Context에 Activity를 참조하게 되면 Activity 가 파괴되더라도 가비지에 의해 정리되지 않기때문에
메모리누수가 발생합니다. 따라서 오래보관해야하는 Context 가 있을때 외에는 사용을 자제할것.
[Application 구조]
참고 :
https://m.blog.naver.com/vps32/222327391678
https://developer.android.com/reference/android/content/Context#summary
반응형
'개발 > Android' 카테고리의 다른 글
[Android] 4대 Component (0) | 2022.07.29 |
---|---|
[Android] Handler? Looper? Runnable? (0) | 2022.07.28 |
[Android] Room 사용기 (0) | 2022.07.28 |
[Android] Fragment 생명주기 (0) | 2022.07.26 |
[Android] Activiy 생명주기 (0) | 2022.07.26 |
댓글