Android Studio

Android Studio) Context란?

zs1397 2023. 1. 6. 17:30

 

 


 

  • 애플리케이션 환경에 대한 글로벌 정보의 인터페이스
  • Application의 현재 상태를 갖고있다.
  • Activity와 Application Class는 Context 클래스를 상속받은 클래스들이다.

 종류

   1. Application Context(애플리케이션 컨텍스트)

  • 애플리케이션 자체 생명주기에 영향을 받는다
  • 애플리케이션 실행되어 종료될 때까지 동일한 객체를 참조
  • Activity에서 getApplicationContext함수를 통해 접근 가능

   2. Activity Context(액티비티 컨텍스트)

  • 엑티비티 라이프사이클과 연결, Activity에서 사용가능
  • 엑티비티 내 컨텍스트 전달 혹은 라이프사이클이 현재의 컨텍스트에 붙은 컨텍스트가 필요할 때 사용
  • Activity에서 사용되는 Dialog, Toast 등의 UI작업은 Activity Context사용

※ Singleton 객체, Application에서 초기화하는 객체일 경우 -> Application Context 사용

   Activity에서 사용되는 UI 컨트롤러 객체 -> Activity Context 사용

 

Android에서 사용되는 Method

  • View.getContext(): 현재 실행되고 있는 View의 context를 리턴
  • Activity.getApplicationContext(): 애플리케이션 context를 return + 애플리케이션의 생명주기에 해당하는 context를 사용
  • ContextWrapper.getBaseContext(): 다른 Context에 접근할 때 사용, ContextWrapper는 context참고 가능
  • this = view.getcontxt()

참고: https://velog.io/@bye9/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EA%B8%B0%EB%B3%B8%EA%B0%9C%EB%85%90

'Android Studio' 카테고리의 다른 글

Android Studio) 안드로이드 구성  (0) 2023.01.06
Android Studio) 기본 구성과 개념  (0) 2023.01.06