๐Ÿ—ฃ ์•ˆ๋“œ๋กœ์ด๋“œ Context

sery270ยท2021๋…„ 4์›” 17์ผ
2

Android

๋ชฉ๋ก ๋ณด๊ธฐ
8/13

์•ˆ๋…•ํ•˜์„ธ์š” :) ์˜ค๋Š˜์€ ์•ˆ๋“œ๋กœ์ด๋“œ Context์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. context : Context ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์š”๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์‹ ์ ์ด ๋งŽ์œผ์‹คํ…๋ฐ์š”. ๊ณผ์—ฐ context๊ฐ€ ๋ฌด์—‡์ด๊ณ  ์™œ ํ•„์š”ํ•œ์ง€, context ์ข…๋ฅ˜๋Š” ์–ด๋–ค๊ฒƒ์ด ์žˆ๊ณ , ๊ฐ ์ข…๋ฅ˜์˜ context๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์€ ์—†๋Š”์ง€ ์ •๋ฆฌํ•ด๋ณด๋ คํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋Š˜๋„ ํ™”์ดํŒ… ์ž…๋‹ˆ๋‹ค ! ๐ŸŒฟ

1๏ธโƒฃ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์ปจํ…์ŠคํŠธ(Context)๊ฐ€ ํ•„์š”ํ•œ ์ด์œ 


  • ํŠน์ • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํŠน์ • ํ”„๋กœ์„ธ์Šค์— mapping๋˜์–ด ์‹คํ–‰๋˜๊ณ  ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ๋‹ค๋ฉด, ๋‹ค๋ฅธ ๋งค๊ฐœ์ฒด๋‚˜ ์ถ”๊ฐ€์ ์ธ ์ •๋ณด ํ•„์š”์—†์ด, ์ž์‹ (์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜)์„ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฌผ์–ด๋ณด๊ฑฐ๋‚˜ ํ”„๋กœ์„ธ์Šค์™€ ์—ฐ๊ด€๋œ ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์•ˆ๋“œ๋กœ์ด๋“œ(์•ˆ๋“œ๋กœ์ด๋“œ ํ”Œ๋žซํผ)์—์„  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ณ„๋„๋กœ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ๊ฐ์˜ ํ”„๋กœ์„ธ์Šค๋Š” OS์˜ ์ปค๋„์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜์ง€๋งŒ, ๊ฐ๊ฐ์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ActivityManagerService์— ์˜ํ•ด ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

app ํ”„๋กœ์„ธ์Šค์™€ system_server ํ”„๋กœ์„ธ์Šค ๋™์ž‘๊ตฌ์กฐ

  • ์ฆ‰, ๊ฐ๊ฐ์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ , ๊ด€๋ฆฌํ•˜๋Š” ์ฃผ์ฒด๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ์•„๋‹Œ, ๋ณ„๋„์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ธ ActivityManagerService์ธ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์•ˆ๋“œ๋กœ์ด๋“œ์—์„  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ง์ ‘์ ์œผ๋กœ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ฑฐ๋‚˜, ๊ธฐ๋Šฅ ํ˜ธ์ถœ์„ ์š”์ฒญํ•˜์ง€ ์•Š๊ณ , ์ด ActivityManagerService๋ฅผ ํ†ตํ•ด์„œ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    • ActivityManagerService์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๋•๊ธฐ์œ„ํ•ด, system_server ํ”„๋กœ์„ธ์Šค์™€ app ํ”„๋กœ์„ธ์Šค์˜ ๋™์ž‘๊ตฌ์กฐ๋ฅผ ์ฒจ์–ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
    • ActivityManagerService๋Š” system_server ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰๋˜๋Š” ์„œ๋ฒ„์ด๊ณ , ActivityManager๋Š” app ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰๋˜๋Š” ํด๋ผ์ด์–ธํŠธ (์”ฌ ํด๋ผ์ด์–ธํŠธ)์ž…๋‹ˆ๋‹ค.
    • ActivityManagerService์—๊ฒŒ ์š”์ฒญ๋œ ์ž‘์—…์€ system_server ํ”„๋กœ์„ธ์Šค์—์„œ ๊ด€๋ฆฌ๋˜๊ณ , ActivityManager์—๊ฒŒ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ActivityManager์—๊ฒŒ ์ „๋‹ฌ๋œ ์ž‘์—…์€ ์‹ค์งˆ์ ์œผ๋กœ app ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
    • ์ฆ‰, "ํ†ตํ•ฉ ๋ฌธ์˜ ์ฑ„๋„"์˜ ์—ญํ• ์„ ํ•˜๋Š” ActivityManagerService๊ฐ€ ์—ฌ๋Ÿฌ ์•ฑ์„ ํ†ตํ•ฉํ•ด์„œ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ, ์ด ๊ด€๋ฆฌ๋ผ๋Š” ๊ฒƒ์ด, ์ž์‹ ์ด(ActivityManagerService๊ฐ€) ๊ฐ๊ฐ์˜ ์•ฑ์—๊ฒŒ(ActivityManager์—๊ฒŒ) ์ž‘์—…์„ ๋ช…๋ นํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Context๋Š” application, activity์— ๋Œ€ํ•œ ์‹ ๋ถ„์ฆ (ID)

  • ActivityManagerService์˜ ๋™์ž‘ ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ดํ•ด ํ•˜์…จ๋‹ค๋ฉด, ์ด์ œ ์™œ context๊ฐ€ ํ•„์š”ํ•œ์ง€ ์ดํ•ดํ•˜์‹ค ์ˆ˜ ์žˆ์œผ์‹ค๊ฒ๋‹ˆ๋‹ค. ActivityManagerService ์ž…์žฅ์—์„œ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ์ž์‹ ์—๊ฒŒ ๋“ค์–ด์˜ค๋Š” ์ •๋ณด ์ ‘๊ทผ ์š”์ฒญ์ด๋‚˜ ์‹œ์Šคํ…œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์š”์ฒญ์ด ๋ˆ„๊ตฌ์˜(์–ด๋Š app์˜) ์š”์ฒญ์ธ์ง€ ํŒŒ์•…ํ•ด์•ผ, ์ œ๋Œ€๋กœ๋œ ๊ณณ์— ์ž‘์—…์„ ๋ช…๋ นํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ž€ ๋ง์ž…๋‹ˆ๋‹ค โ€ผ๏ธ

  • ์—ฌ๊ธฐ์„œ "๋ˆ„๊ตฌ์˜ ์š”์ฒญ"์ธ์ง€๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ œ๊ณต๋˜๋Š” ์ •๋ณด๊ฐ€ ๋ฐ”๋กœ Context ์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ application, activity์— ๋Œ€ํ•œ ์‹ ๋ถ„์ฆ (ID)๋กœ ์ดํ•ดํ•˜์‹œ๋ฉด ์‰ฝ์Šต๋‹ˆ๋‹ค.

  • ๊ด€๋ จํ•ด์„œ ์–ด๋–ค ์œ ์ตํ•œ ๊ธ€์—์„  Context๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ๋ฅผ Context์˜ ์—ญํ• ๋กœ ์ •๋ฆฌ๋ฅผ ํ•ด์ฃผ์…จ๋Š”๋ฐ, ๋ช…์พŒํ•˜์—ฌ ๋ฌธ์žฅ์„ ์ธ์šฉํ•ด๋ด…๋‹ˆ๋‹ค.

    ์ž์‹ ์ด ์–ด๋–ค ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ID ์—ญํ• 

    ActivityManagerService ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ†ต๋กœ ์—ญํ• 

    • ์ œ ๋ฌธ์žฅ์œผ๋กœ ๋ฐ”๊พธ์–ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
      • ์ž์‹ ๊ณผ ๊ด€๋ จ๋œ ์ •๋ณด, ์ƒํƒœ๋ฅผ ์–ป๊ธฐ์œ„ํ•ด, ์•Œ๋ ค์ฃผ์–ด์•ผํ•  ์ž์‹ ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ Context
      • ์‹œ์Šคํ…œ ์ž์›์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋งค๊ฐœ, ํ•ธ๋“ค๋Ÿฌ์ธ Context

2๏ธโƒฃ ์•ˆ๋“œ๋กœ์ด๋“œ์˜ ์ปจํ…์ŠคํŠธ(Context)


  • ๊ณต์‹๋ฌธ์„œ์˜ overview๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด, Context๋Š” ์•„๋ž˜ ๋ฌธ์žฅ๋“ค๋กœ ์š”์•ฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • Context ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฉฐ, ์ด์˜ ๊ตฌํ˜„์€ ์•ˆ๋“œ๋กœ์ด๋“œ ์‹œ์Šคํ…œ์ด ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    • Context๋Š” application ํ™˜๊ฒฝ์— ๊ด€ํ•œ ๊ธ€๋กœ๋ฒŒ ์ •๋ณด๋ฅผ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.
    • Context๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํŠนํ™”๋œ ๋ฆฌ์†Œ์Šค๋‚˜ ํด๋ž˜์Šค์— ์ ‘๊ทผํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
    • Context๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ ˆ๋ฒจ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ API๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
      • Activity ์‹คํ–‰, Intent ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…, Intent ์ˆ˜์‹  ๋“ฑ๋“ฑ
  • ํ•œ๋งˆ๋””๋กœ, Context๋Š” ๊ฐ๊ฐ์˜ application, activity์— ๋Œ€ํ•œ ์ •๋ณด ๋ฐ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๊ฐ€์ง„ ๊ฐ์ฒด๋ผ๊ณ  ์ดํ•ด๋ฉ๋‹ˆ๋‹ค ! ์ฆ‰,

    • Context๋ฅผ ํ†ตํ•ด Activity ๊ทธ๋ฆฌ๊ณ  Application์˜ ์ •๋ณด, ์ƒํƒœ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Context๋ฅผ ํ†ตํ•ด Resource, Database, SharedPrefernces ๋“ฑ์˜ ์‹œ์Šคํ…œ ์ž์›์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์•ˆ๋“œ๋กœ์ด๋“œ Context๋Š” Application Context์™€ Activity Context๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ(Application Context)


  • Application Context๋Š” ์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค ์ž…๋‹ˆ๋‹ค.

    • ์ด Application Context (์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค)๋Š” getApplicationContext()์„ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Application Context๋Š” Application์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋ฌถ์—ฌ์žˆ์Šต๋‹ˆ๋‹ค.

    • ๋”ฐ๋ผ์„œ, Application Context๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

      • ํ˜„์žฌ์˜ Context์™€ ๋ถ„๋ฆฌ๋œ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง„ Context๊ฐ€ ํ•„์š”ํ•  ๋•Œ

      • Activity ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ Context๋ฅผ ์ „๋‹ฌํ•  ๋•Œ

        ex) Application์—์„œ ์ƒ์„ฑํ•œ ์‹ฑ๊ธ€ํ„ด ๊ฐ์ฒด์— Context๊ฐ€ ํ•„์š”ํ•  ๋•Œ

        ex) Activity์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด์•ผํ•  ๋•Œ

4๏ธโƒฃ ์•กํ‹ฐ๋น„ํ‹ฐ ์ปจํ…์ŠคํŠธ(Activity Context)


  • Activity Context๋Š” Activity์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋ฌถ์—ฌ์žˆ์Šต๋‹ˆ๋‹ค.

    • ๋”ฐ๋ผ์„œ, Activity Context๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

      • ํ˜„์žฌ์˜ Context์™€ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๋Š” ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง„ Context๊ฐ€ ํ•„์š”ํ•  ๋•Œ

      • Activity ๋ฒ”์œ„ ๋‚ด์—์„œ Context๋ฅผ ์ „๋‹ฌํ•  ๋•Œ

        ex) Activity ์ƒ๋ช…์ฃผ๊ธฐ์— ์ข…์†๋œ ๊ฐ์ฒด์— Context๊ฐ€ ํ•„์š”ํ•  ๋•Œ

5๏ธโƒฃ Context์™€ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜, ๊ทธ๋ฆฌ๊ณ  "Nearest Context"


Application Context์™€ Activity Context์˜ ๊ด€๊ณ„

  • (์œ„ ๊ทธ๋ฆผ ์ถœ์ฒ˜) ์•ˆ๋“œ๋กœ์ด๋“œ Context๋Š” Application Context์™€ Activity Context๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.
  • Application Context๋Š” ๊ฐ Application์— ์œ ์ผํ•˜๋ฉฐ, ์ข…์†๋ฉ๋‹ˆ๋‹ค.
  • Activity Context๋Š” ๊ฐ Activity์— ์œ ์ผํ•˜๋ฉฐ, ์ข…์†๋ฉ๋‹ˆ๋‹ค.
  • Activity๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ Application์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ์š”์†Œ๋กœ, Application์€ 1๊ฐœ ์ด์ƒ์˜ Activity๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ฆ‰ Activity๋Š” Application์— ์ข…์†๋ฉ๋‹ˆ๋‹ค.
    • ๋”ฐ๋ผ์„œ Application Context ๋ฒ”์œ„ ์•ˆ์— Activity Context๊ฐ€ ์กด์žฌํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค.
  • ์—ฌ๊ธฐ์„œ "Nearest Context" ๊ฐœ๋…์ด ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.

"Nearest Context"

  • MyApplication์˜ "Nearest Context" : Application Context

  • MainActivity1์˜ "Nearest Context" : MainActivity1 Context

    (Application Context, MainActivity1 Context ์ค‘์—์„œ )

  • MainActivity2์˜ "Nearest Context" : MainActivity2 Context

    (Application Context, MainActivit21 Context ์ค‘์—์„œ )

Context์™€ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜

6๏ธโƒฃ ์ถ”์ƒ ํด๋ž˜์Šค Context


(์œ„ ๊ทธ๋ฆผ ์ถœ์ฒ˜)

#๏ธโƒฃ Reference


profile
๊ฐœ๋ฐœ์„ธ๋ฆฌ์˜ ์„ฑ์žฅ๊ธฐ๐ŸŒฟ

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