About Context

이영훈·2021년 3월 18일
0

Context

Interface of global inforamtion about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-spectific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

-> 간단하게 "Android 시스템에서 제공하는 추상 클래스이고, Activity, Resource등에 접근할 수 있도록 해준다" 라는 내용 같다.

Activity 객체는 Context 객체를 상속 받는다.

즉 Activity는 애플리케이션의 특정 자원, 클래스, 환경 정보 등에 대해 접근할 수 있게 된다.

getApplicationContext, getActivity 등 분명한 차이점이 있겠지만 지금까지 확실한 차이점을 알고 사용하지 못했던것 같아

확실히 짚고 넘어가려 한다!


Application Context

  • 애플리케이션 전체의 생명주기와 관련되어 있음.
  • 싱글톤
  • getApplicationContext()를 통해 Activity에서 접근 가능
  • 현재의 context나 activity의 범위를 벗어난 작업에 필요함
  • 가장 오래 지속될 Context가 필요할때만 사용할 것.

싱글톤 객체를 생성했는데 그 객체에 Context가 필요하다면, getApplicationContext()를 사용하면 된다!

이런 상황에서 Activity의 Context를 사용한다면, 메모리 누수가 발생하게 된다.

Application Context에 Activity를 참조하게 되면 Activity가 부서지더라도 참조가 유지되므로, Activity가 GC(가비지 콜렉팅)되지 않으므로 메모리 누수가 발생한다.


Activity Context

  • 액티비티가 사라지면 Context도 같이 사라진다.
  • 액티비티의 생명 주기와 연관 되어있다.

일단 getApplicationContext와 Activity의 Context의 차이점을 간략하게나마 확인해봤다!

많은 내용은 아니지만 개인적으로는 도움이 된 것 같다.

devlog of ShinJe Kim

찰스의 안드로이드

두 블로그의 포스팅을 참고하였다!

profile
And dev

0개의 댓글