[Android] Context

ErroredPasta·2022년 5월 6일
0

Android

목록 보기
4/5

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

Context는 application 환경에 대한 전역 정보의 interface입니다. 즉, 특정 application의 resource, 클래스 등에 접근할 수 있도록 해주는 것이 Context입니다.

Activity context vs. application context

  • Application context
    Application context는 현재 프로세스가진 Application singleton의 Context입니다. 현재 Context의 생명주기에서 벗어나 Application의 lifecycle과 함께 할때 사용해야합니다. 그렇지 않으면 memory leak이 발생할 수 있습니다.

  • Activity context
    Activity context는 Activity가 가지는 ContextActivity의 생명주기를 같이합니다.

Activity(or application) vs. base context(or application context)

Context가 필요할 때, ActivityApplication을 넘겨줄 수도 있지만 baseContextapplicationContext를 넘겨줄 수도 있습니다. Context가 필요한대 ActivityApplication을 넘겨줄 수 있는 이유는 둘 다 Context를 상속받기 때문입니다.

// ContextThemeWrapper extends ContextWrapper
public class Activity extends ContextThemeWrapper implements ...
// ContextWrapper extends Context
public class Application extends ContextWrapper implements ComponentCallbacks2

ContextActivityApplication를 넘겨주는 것과 두 클래스의 base context를 넘겨주는 것에 대해 동작의 차이는 없습니다. 그 이유는 Activity혹은 Application를 넘겨줄 경우 내부에 존재하는 base context에게 동작을 delegate하고 base context를 넘겨주면 받은 context를 그대로 사용하기 때문에 둘 다 base context를 이용하기 때문입니다.

Context 종류

Non-UI context

Indicates this Context can not handle UI components properly and is not associated with a Display instance.

Display instance와 아무런 연관이 없는 Context로 UI 구성 요소들을 처리할 수 없습니다.

Display context

Indicates this Context is associated with a Display instance but should not be handled UI components properly because it doesn't receive configuration changes regardless of display property updates.

ContextDisplay instance와 연관이 있지만 configuration change를 받지 않으므로 UI 구성 요소들을 처리하면 안됩니다.

Activity context

Indicates this Context is an Activity or Activity derived Context.

Activity이거나 Activity에서 파생된 Context입니다.

Window context

Indicates this Context is a WindowContext or WindowContext derived Context.

WindowContext이거나 WindowContext에서 파생된 Context입니다.

System or system UI context

Indicates this Context is created from createSystemContext(ActivityThread) or createSystemUiContext(ContextImpl, int) or any Context that system UI uses.

createSystemContextcreateSystemUiContext로 생성되거나, 혹은 시스템 UI를 사용하는 Context입니다.

etc.

아무런 타입을 지정하지 않은 Context입니다. 대표적으로 applicationContext가 있습니다.

UI context와 non-UI context

위의 context 종류에서 UI context와 non-UI context로 나눌 수 있습니다.

// SDK 31
@Override
public boolean isUiContext() {
    switch (mContextType) {
        case CONTEXT_TYPE_ACTIVITY:
        case CONTEXT_TYPE_WINDOW_CONTEXT:
        case CONTEXT_TYPE_SYSTEM_OR_SYSTEM_UI:
            return true;
        case CONTEXT_TYPE_DISPLAY_CONTEXT:
        case CONTEXT_TYPE_NON_UI: {
            return false;
        }
        default:
            return false;
    }
}

Context 중 Activity, Window, System or system UI context는 UI Context임을 확인할 수 있습니다.

Context 사용 [2]

  1. Application을 이용하여 Activity를 시작할 수 있지만 새로운 task를 생성해야합니다. 특정 상황에 적합할 수도 있으나, 일반적이지 않은 back stack 동작을 만들 수 있으며 일반적으로 권장되지 않습니다.

  2. 가능하지만 application에 정의된 theme이 아닌 기본 theme을 사용하여 inflate됩니다.

  3. Android 4.2 이상에서 receiver가 null일 경우 sticky broadcast의 현재 값을 얻기위해 사용됩니다.

여기에서 ContentProviderBroadcastReceiverContext가 아닙니다. 하지만 ContentProvider는 생성시 Context를 받고 getContext를 이용하여 Context를 가져올 수 있습니다. BroadcastReceiver는 새로운 broadcast event 발생시 프레임워크에서 ReceiverRestrictedContext를 넘겨줍니다.

Reference

[1] "Context," Android Developers, last modified Apr 26, 2022, accessed May 9, 2022, https://developer.android.com/reference/android/content/Context.

[2] "Context, What Context?," POSSIBLE Mobile. last modified n.d., accessed May 11, 2022, https://web.archive.org/web/20150329210012/https://possiblemobile.com/2013/06/context/.

profile
Hola, Mundo

0개의 댓글