다이얼로그에 왜 application context를 사용하면 안될까?
https://stackoverflow.com/a/10159345
Show dialog only using Context instead of Activity instance
I could show dialog if I uses an Activity instance but when I uses Context or Application Context instance Dialog is not showing. AlertDialog.Builder builder = new AlertDialog.Builder(activity); ...
stackoverflow.com
Toast를 제외하고 다른 UI 구성 요소는 Application 컨텍스트와 함께 작동하지 않습니다. [android 2.1 때 변경]
응용 프로그램 컨텍스트는 응용 프로그램과 관련된 일부 서비스 또는 구성 요소 (예 : 전화 통신 관리자, 위치 관리자 등)가 필요할 때 항상 전달됩니다. UI의 경우 항상 Activity 인 UI 관련 컨텍스트를 전달해야합니다.
ApplicationContext는 시스템과 관련된 기능과 앱 전체의 생명주기와 관련되어 있기 때문에 dialog처럼 해당 activity에 종속되는 뷰는 해당 activity의 context를 사용해야 한다.
Context의 종류
-
View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
-
Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
-
ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().