Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

개발로 개발해

안드로이드 Context 본문

android

안드로이드 Context

Dev_JS 2018. 7. 6. 11:31

안드로이드 프레임워크에서 Context는 2가지 종류로 나뉜다.

  • 애플리케이션 Context
  • 액티비티 Context

애플리케이션 Context는 애플리케이션 자체의 생명주기(라이프사이클)에 영향을 받는다. 따라서 항상 애플리케이션의 생명 주기와 함께 한다. 

그러나 엑티비티 Context는 액티비티의 라이프사이클과 함께 작동해, onDestory()와 함께 사라진다. 즉 액티비티에 대한 환경 정보들이 Context에 있고, 이 Context(엑티비티의)에 Intent를 통해 다른 액티비티를 띄우면, 엑티비티 스택이 쌓이게 되는 것이다.

LoginActivity.this though its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context. 
getApplication() though its referring to Application object but the Application class extends Context class, so it can be used to offer application context. 
getApplicationContext() offers application context. 
getBaseContext() offers activity context.

세부적인 설명인데, 여기서 중요한 것은 안드로이드의 Avtivity와 Apllication class들이 모두 Context클래스를 상속받아 만들어졌다는 것이다. 그래서 LoginActivity.this라고 하는 순간, this에 해당하는 LoginActivity인스턴스가 파라매터로 전달되고, 이것은 Context를 상속받았기에 Context이며, 따라서 아무런 문제 없이 작동하게 되는 것이다.

Tips: 뷰를 수정하거나 할때는 Activity Context를 나머지 경우에는 Application Context를 사용하라.



'android' 카테고리의 다른 글

아이콘 변경 사이트  (0) 2018.07.08
setContentView  (0) 2018.07.06
Toast Class  (0) 2018.07.06
Comments