[Android] Context

๐Ÿ’ก ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์ž์ฃผ ์ ‘ํ•˜๋Š” ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•˜์—ฌ ํ•™์Šตํ•˜์˜€์Šต๋‹ˆ๋‹ค!

 

Context

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

Android Context

  • ์•ˆ๋“œ๋กœ์ด๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ์— ๋Œ€ํ•œ ๊ธ€๋กœ๋ฒŒ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์‹œ์Šคํ…œ ์„œ๋น„์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” API์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ๋ฆฌ์†Œ์Šค, DB, Shared Preferences
  • Activity, Application Context๋Š” Android Context ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

Application Context

  • Activity์—์„œ applicationContext๋ผ๋Š” ํ”„๋กœํผํ‹ฐ(or getApplicationContext())๋ฅผ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค.
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋‹จ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ผ์ดํ”„์‚ฌ์ดํด๊ณผ ๋ฌถ์—ฌ์žˆ์œผ๋ฉฐ, ์•„๋ž˜์™€ ๊ฐ™์€ ์ž‘์—…์— ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.
    • Context๊ฐ€ ์ข…๋ฃŒ๋˜๊ณ  ๋‚˜์„œ๋„ Context๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—…
    • ์•กํ‹ฐ๋น„ํ‹ฐ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ณณ์—์„œ Context๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—…
// ๋กœ์ปฌ data๋ฅผ ์ €์žฅํ•˜๋Š” addressModule๋ฅผ ํ™œ์šฉํ•  ๋•Œ context๋ฅผ ๋„˜๊ฒจ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
// ์ด๋Š” ์˜ค๋žซ๋™์•ˆ ์ง€์†๋˜๊ฑฐ๋‚˜ ์•ฑ ์ „์—ญ์—์„œ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ applicationContext๋ฅผ ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
private val addressModule = AddressModule(context)

์ž˜๋ชป ๋œ Context๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์— ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ Activity Context๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์— Activity Context๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด, Activity์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋‚จ๊ฒจ๋‘๊ฒŒ ๋˜๊ณ , GC ๋˜์ง€ ์•Š์€ ์ฑ„ ๋ฉ”๋ชจ๋ฆฌ ๋ฆญ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    • [GC] : Carbage Collected๋กœ ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•ด์คŒ
    • [๋ฉ”๋ชจ๋ฆฌ ๋ฆญ] : ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณ„์† ์ ์œ ํ•˜๋Š” ํ˜„์ƒ
  • ์˜ค๋žซ๋™์•ˆ ์ง€์†๋˜๊ฑฐ๋‚˜ ์•ฑ ์ „์—ญ์—์„œ ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ์—๋งŒ Application Context๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ ํ˜„์žฌ์˜ ์ปจํ…์ŠคํŠธ์™€ ๋ถ„๋ฆฌ๋œ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๊ฐ€์ง„ ์ปจํ…์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜, ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„  ์ปจํ…์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Activity Context

  • Activity์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋ฉฐ Activity์˜ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ๊ด€๋ จ์ด ์žˆ๋Š” Context์ž…๋‹ˆ๋‹ค.
// context -> context๋กœ ์ด๋™ํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
// activity context๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ด๋™ํ•˜๊ธฐ ์ „์ธ activity์˜ ์ •๋ณด๋ฅผ ๋„˜๊ฒจ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
companion object {
    fun getIntent(context: Context): Intent {
        return Intent(context, ClubAddActivity::class.java)
    }
}
  • ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ํŒŒ๊ดด๋˜๋ฉด Context๊ฐ€ ์†Œ๋ฉธ๋˜๋ฉฐ, ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ๊ณผ ์•กํ‹ฐ๋น„ํ‹ฐ ๊ด€๋ จ ์ž‘์—…์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • Application Context๋ณด๋‹ค ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์†Œ๋น„ํ•˜๊ณ , ์•กํ‹ฐ๋น„ํ‹ฐ ์ƒ๋ช…์ฃผ๊ธฐ์— ์˜ํ–ฅ์„ ๋ฐ›์œผ๋ฏ€๋กœ ์‚ฌ์šฉ ์‹œ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

Application Context๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ

  • Application Context๋Š” Activity Context๊ฐ€ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • GUI (View Component ๊ด€๋ จ ๋™์ž‘) ์—์„œ Application Context๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค.
    • ex> Application Context๋กœ AlertDialog๋ฅผ show()ํ•œ ๊ฒฝ์šฐ
  • Activity๋Š” Garbage Collection์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, Application Context๋Š” ์•ฑ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‚ด์•„์žˆ๋Š” ๋™์•ˆ ๊ณ„์† ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ด๋Š” Application Context๋ฅผ ํ™œ์šฉํ•œ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ• ๋‹น ํ•ด์ œํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ ๋ฆญ์œผ๋กœ ์ด์–ด์งˆ ํ™•๋ฅ ์ด ํฝ๋‹ˆ๋‹ค.

์—„์ง€ ์†๊ฐ€๋ฝ์˜ ๋ฒ•์น™

  • ์—„์ง€ ์†๊ฐ€๋ฝ์˜ ๋ฒ•์น™(The Rule Of Thumb)์€ ๊ฐ„๋‹จํ•˜๊ณ  ์ง๊ด€์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.
  • ํ˜„์žฌ ์ž‘์—…์ค‘์ธ ๊ฒƒ์„ ๋‘˜๋Ÿฌ์‹ธ๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ปจํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ฐธ์กฐ๊ฐ€ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๋„˜์–ด์„œ์ง€ ์•Š๋Š” ์ด์ƒ ์ฐธ์กฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Appication Context๊ฐ€ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ

  • Application Context๋Š” UI ์š”์†Œ์™€ ์ง์ ‘์ ์œผ๋กœ ๊ด€๋ จ๋œ ์ž‘์—…์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • Dialog, Toast, View ์ƒ์„ฑ ๋“ฑ์˜ ๋กœ์ง์— Application Context๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ WindowManager ๊ด€๋ จ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋„ ์ ์ ˆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค !
    • Activity ๋ฐ ํ™”๋ฉด ์ „ํ™˜ ๊ด€๋ จ ์ž‘์—…์— Application Context๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ
    • ํŠน์ • ๋ทฐ ์ƒ๋ช… ์ฃผ๊ธฐ์™€ ์—ฐ๊ฒฐ๋œ ์ž‘์—…
      • ex> ์• ๋‹ˆ๋ฉ”์ด์…˜, ๋ทฐ ํŠธ๋ฆฌ ๋“ฑ

์ •๋ฆฌ

  • ํ•ญ์ƒ ๊ฐ€์žฅ ๊ฐ€๊น๊ณ  ๋ฐ€์ ‘ํ•œ ์Šค์ฝ”ํ”„์˜ Context๋ฅผ ๊ณจ๋ผ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
  • ์•กํ‹ฐ๋น„ํ‹ฐ ๋‚ด๋ถ€์—์„œ๋Š” Activity Context๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ(์‹ฑ๊ธ€ํ†ค)์—์„œ๋Š” Application Context๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ 

https://velog.io/@haero_kim/Android-Context-๋„ˆ-๋Œ€์ฒด-๋ญ์•ผ

https://developer.android.com/reference/android/content/Context

https://shinjekim.github.io/android/2019/11/01/Android-context๋ž€/