티스토리 뷰

Programming/Android

[Android] Context

RosyPark 2020. 12. 3. 00:34

안드로이드 구성요소(4대 컴포넌트 - Activity, Service, 콘텐스 제공자, 방송 수신자) 가 실행되고 Application에 실행중인 다른 구성요소가 없으면 Android System은 실행스레드로 애플리케이션의 Linux 프로세스를 시작함 

기본적으로 Application의 구성 요소는 같은 프로세스와 같은 스레드에서 실행됨. 일반적인 코딩 프로그램에는 Application와프로세스가 긴밀히 연결되어 있지만 Android의 경우 어플리케이션과 프로세스가 분리되어 있음.

 

Context

-  Android 시스템에 의해 제공되는 추상클래스 --> Application, Service, Activity는 모두 Context를 상속받음

- Context를 잘못 사용하면 메모리 누수로 이어짐 

- Application 환경에 관한 글로벌 정보를 접근하기 위한 인터페이스

- Context의 경우 Application이 시작될때, Application컴포넌트들이 생성될때마다 태어남 

- 기본적으로 시스템함수를 호출하기 위해서는 직접 호출하지만 Android에서는 Context를 인스턴스화 매개체를 통해서 실행함 

(1) Application에 관해서 시스템이 관리하고 있는 정보에 접근하고,

(2) 안드로이드 시스템 서비스에서 제공하는 API를 호출할 수 있는 기능을 가짐 

 

 

 

 

Context 종류

- Application Context, Activity Context는 Context를 상속받아서 만들어짐 

 

1. Application Context

Application 자체의 생명주기에 영향을 받음 

getApplicationContext()를 통해 Activity에서 접근 할 수 있음

 

2. Activity Context 

Activity Life cycle과 함께 작동, onDestroy()와 함께 사라짐

하나의 Application에서 여러 Activity가 존재하는데 특정 Activity Context 를 다룰 수 있음

 

 

 

View.getContext()

현재 실행되고 있는 View의 context를 return 하는데 보통은 현재 활성화된 activity의 context가 된다.

 

Activity.getApplicationContext()

어플리케이션의 Context가 return된다. 현재 activiy의 context 뿐만 아니라 application의 lifeCycle에 해당하는 Context가 사용된다.

 

ContextWrapper.getBaseContext()

자신의 Context가 아닌 다른 Context를 access하려 할 때 사용한다. ContextWrapper는 getBaseContext()를 경유해서 Context를 참조할 수 있다.

 

this

View.getContext()와 같다.

 

 

 

 

<출처>

1. blog.naver.com/PostView.nhn?blogId=huewu&logNo=110085457720

2. maternalgrandfather.tistory.com/entry/this%EC%99%80-getApplicationContext%EC%9D%98-%EC%B0%A8%EC%9D%B4%EC%A0%90%EC%9D%80

3. hashcode.co.kr/questions/666/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C%EC%97%90%EC%84%9C-getcontext-getapplicationcontext-getbasecontext-this%EC%9D%98-%EC%B0%A8%EC%9D%B4%EA%B0%80-%EB%AD%94%EA%B0%80%EC%9A%94

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함