[Android] Context

Minji Jeongยท2022๋…„ 4์›” 27์ผ
1

Android

๋ชฉ๋ก ๋ณด๊ธฐ
6/37
post-thumbnail

๐Ÿ˜ ์–ธ์ œ ์–ด๋””์„œ๋“  ์ •๋ง ์ž์ฃผ ์“ฐ์ด๋Š” context !

์šฐ๋ฆฌ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ context๋ฅผ ์ •๋ง ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ๋‚˜, ํ† ์ŠคํŠธ ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ, ๋ทฐ์™€ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ๊ตฌํ˜„ํ•  ๋•Œ ๋“ฑ๋“ฑ ์ •๋ง ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. ์•ˆ๋“œ๋กœ์ด๋“œ๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šธ ๋•Œ๋งŒ ํ•ด๋„ context์— ๋Œ€ํ•ด์„œ ์ „ํ˜€ ์˜๋ฌธ์„ ๊ฐ€์ง€์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ–ˆ์œผ๋‚˜, ๋งค์šฐ ์ค‘์š”ํ•œ ์š”์†Œ์ด๊ธฐ๋„ ํ•˜๊ณ  ๋ธ”๋กœ๊ทธ๋ฅผ ์˜ฎ๊ธฐ๋Š” ๊น€์— ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด์•ผ ํ•ด์„œ ์ด๋ ‡๊ฒŒ context์— ๋Œ€ํ•ด ๊ธ€์„ ์“ฐ๊ฒŒ ๋˜์—ˆ๋‹ค.

โœ… What is Context in Android ?

Context๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ์— ๋Œ€ํ•ด ์ „์—ญ์ ์ธ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ด๋‹ค. context๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŠน์ • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž์›๊ณผ ํด๋ž˜์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ์ธํ…ํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ๋“ฑ ๋‹ค๋ฅธ ์•ˆ๋“œ๋กœ์ด๋“œ ์ปดํฌ๋„ŒํŠธ๋“ค๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ์ €๋ฆฌ์ฃผ์ €๋ฆฌ ์„ค๋ช…ํ–ˆ์ง€๋งŒ context๊ฐ€ ๊ฐ–๋Š” 4๊ฐ€์ง€์˜ ํŠน์ง•์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ™”๋ฉด์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ณ , ๊ฐ ํ™”๋ฉด์€ ํ•ด๋‹น ํ™”๋ฉด์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” context๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

2. ์•กํ‹ฐ๋น„ํ‹ฐ๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
ํŠน์ • ํ™”๋ฉด์˜ context๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์•กํ‹ฐ๋น„ํ‹ฐ ๋˜๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ์— ์žˆ์Œ์„ ๊ฐ„์ฃผํ•˜๊ณ , context๋ฅผ ์‚ฌ์šฉํ•ด ํ™”๋ฉด๊ณผ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

3. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฆฌ์†Œ์Šค, Local Database ๋˜๋Š” DataStore์™€ ๊ฐ™์€ ๋กœ์ปฌ ์ €์žฅ์†Œ์— ์ ‘๊ทผํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
๊ฐ ํ™”๋ฉด์— ๋Œ€ํ•œ context๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

4. ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ context๋ฅผ ์ƒ์†ํ•œ๋‹ค.
context๋Š” ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์•กํ‹ฐ๋น„ํ‹ฐ๋Š” ๊ฐ๊ฐ์˜ ํ™”๋ฉด์„, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์•ฑ ์ „์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋ฉฐ ๋‘˜ ๋‹ค context๋ฅผ ์ƒ์†ํ•œ๋‹ค.

GeeksForGeeks์—์„œ context์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๋Š” ์ข‹์€ ์˜ˆ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

ํ•œ ์‚ฌ๋žŒ์ด ํ˜ธํ…”์— ๋ฉฐ์น  ๋ฌต๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋Š” ์•„์นจ, ์ ์‹ฌ, ์ €๋…์„ ๋•Œ์— ๋งž์ถฐ ๋จน์–ด์•ผ ํ–ˆ๋‹ค. ๊ทธ๊ฐ€ ๋ผ๋‹ˆ๋ฅผ ์ œ๋•Œ ์ฑ™๊ฒจ ๋จน์œผ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ๋‹น์—ฐํžˆ ํ”„๋ก ํŠธ ์ง์›์—๊ฒŒ ๊ฐ€์„œ ์‹์‚ฌ๋ฅผ ๊ฐ–๋‹ค๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์•ˆ๋“œ๋กœ์ด๋“œ ํ™˜๊ฒฝ์— ๋น„์œ ํ•œ๋‹ค๋ฉด ์‹์‚ฌ๋ฅผ ์š”์ฒญํ•˜๋Š” ์‚ฌ๋žŒ์€ ๋‹จ์ผ Activity, ํ”„๋ก ํŠธ ์ง์›์€ context, ํ˜ธํ…”์€ App์ด ๋˜๊ณ , ์‹์‚ฌ๋Š” Resources๊ฐ€ ๋œ๋‹ค.

์œ„ ์˜ˆ์ œ๋ฅผ ํ† ๋Œ€๋กœ, context๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋‚˜ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ •๋ณด(์ž์›)๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ์œ ์ถ”ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๐ŸŸข Context Type

context๋Š” ํฌ๊ฒŒ Application Context, Activity Context 2๊ฐ€์ง€ ํƒ€์ž…์ด ์žˆ๋‹ค.

Application Context
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ์—์„œ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค๋กœ ํ”„๋กœ์„ธ์Šค์—์„œ 1๊ฐœ๋งŒ ์กด์žฌํ•˜๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ƒ์„ฑ๋˜๊ณ  ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์กด์žฌํ•œ๋‹ค. Application Context๋Š” getApplicationContext() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ํ˜„์žฌ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ข…๋ฃŒ๋œ ์ดํ›„์—๋„ ์ปจํ…์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—…์ด๋‚˜ ์•กํ‹ฐ๋น„ํ‹ฐ ์Šค์ฝ”ํ”„๋ฅผ ๋ฒ—์–ด๋‚œ ์ปจํ…์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—…์— ์ ํ•ฉํ•˜๋‹ค.

Activity Context
์•กํ‹ฐ๋น„ํ‹ฐ ๋‚ด์—์„œ ์œ ํšจํ•œ Context์ด๊ธฐ ๋•Œ๋ฌธ์— ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ๋™์ผํ•œ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค. Toast๋‚˜ Dialog ๊ฐ™์€ UI ๊ด€๋ จ ์ž‘์—…์„ ์œ„ํ•ด ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. Activity Context๋Š” this ๋˜๋Š” getBaseContext(), getContext()๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

โœ” ํ•„์š”ํ•œ Context๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” methods
getApplicationContext()
Application Context๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
getContext()
ํ˜„์žฌ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ๋ทฐ์˜ Context๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
getBaseContext()
์ž์‹ ์˜ Context๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ Context์— ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ”ต Activity์™€ Fragment์—์„œ์˜ Context ์ ‘๊ทผ ๋ฐฉ๋ฒ•

Activity๋Š” Context๋ฅผ ์ƒ์†ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— this๋กœ ๋„˜๊ธฐ๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, Fragment๋Š” Context๋ฅผ ์ƒ์†ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— this๋กœ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ Fragment์˜ context๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด getContext()๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

โ” getContext() vs requireContext()

@NonNull
public final Context requireContext() {
	Context context = getContext();
    if (context == null) {
    	throw new IllegalStateException("Fragment " + this + " not attached to a context.");
    }
    return context;
}
@Nullable
public Context getContext() {
	return mHost == null ? null : mHost.getContext();
}

getContext()์— ๋Œ€ํ•ด ๊ฒ€์ƒ‰ํ•ด๋ณด๋˜์ค‘, requireContext()๋กœ๋„ context๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋‹ค๋งŒ ์ด ๋‘๊ฐ€์ง€์˜ ๋ฉ”์„œ๋“œ๋Š” ๊ฐ๊ฐ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”๋ฐ, getContext()์˜ ๊ฒฝ์šฐ context()๊ฐ€ ํ˜ธ์ŠคํŠธ์— ๋ถ™์–ด์žˆ์ง€ ์•Š์„ ๋•Œ null์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, requireContext()์˜ ๊ฒฝ์šฐ getContext()์—์„œ ๋ฐ˜ํ™˜๋œ context๊ฐ€ null์ผ ๊ฒฝ์šฐ illegalStateException์„ throwํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Fragment์—์„œ context์— ์ ‘๊ทผํ•˜๋ฉด null์ด ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, fragment๊ฐ€ activity์— attach ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋˜๋„๋ก์ด๋ฉด requireContext()๋ฅผ ํ†ตํ•ด context๊ฐ€ null์ด ์•„๋‹˜์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ† ์ŠคํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๋ฉด,

public static StyleableToast makeTextโ€‹(@NonNull
                                      Context  context,
                                      String  text,
                                      @StyleRes
                                      int style)

makeText()์˜ context ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” Null ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ getContext()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ?.let ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด context๊ฐ€ null์ด ์•„๋‹ ๋•Œ๋งŒ makeText()๋ฅผ ์‹คํ–‰ํ•˜๋ผ๊ณ  ๊ฒฝ๊ณ ๋ฅผ ๋„์šด๋‹ค.

ํ•˜์ง€๋งŒ ์•„๋ž˜์™€ ๊ฐ™์ด requireContext()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด context๊ฐ€ null์ด ์•„๋‹˜์„ ๋ณด์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

References

https://developer.android.com/reference/android/content/Context
https://www.geeksforgeeks.org/what-is-context-in-android/
https://stackoverflow.com/questions/24941572/difference-in-context-for-activity-and-fragment
https://minchanyoun.tistory.com/58

profile
Flutter Developer

0๊ฐœ์˜ ๋Œ“๊ธ€