Mobile

[Android] Context

ㅋ. ㅋ 2021. 8. 20. 11:29

컨텍스트 관련 잘 정리된 글을 보고 간단히 정리

 

Understanding Context In Android Application

What is Context? As there are different types of context in Android, we as an Android Developer often get confused about which context to use at which place. So let’s understand what are those, how to use those and when to use which one.

blog.mindorks.com

 

Context 컨텍스트는 무엇인가

어플리케이션의 현재 상태

activity, application 관련 정보를 가짐 - resources, db 등

 

 

Application Context

getApplicationContext()를 통해 사용 가능

application의 생명주기를 따름

ex. 싱클톤 객체를 생성했을 시에는 application context 를 사용

activity context 사용 시, 어플 종료 시 까지 activity context를 갖고 있기 때문에 activity가 garbage collection이 안돼서 메모리 누수가 일어남

 

 

Activity Context

activity 내에서 사용가능한 context.

activity의 생명주기를 따름

 

 

언제 어떤 Context를 사용해야하는가

Application Context - 싱글턴 객체 (ex. DB, WorkManager 등), Service

Activity Context - toast, dialogs 등 액티비티 UI 관련 기능