๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ| Android/๐Ÿš€ | Jetpack

[Android, Kotlin] Android Activity์˜ lifeCycle ์ •๋ฆฌ

by immgga 2023. 3. 17.

์•ˆ๋“œ๋กœ์ด๋“œ๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ๋ณด๋ฉด activity, fragment์˜ ์ƒํƒœ์— ๋”ฐ๋ฅธ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด life cycle ํ•จ์ˆ˜๋“ค์ธ๋ฐ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ทธ๊ฒƒ๋“ค์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž.

 

life cycle์ด๋ž€?

activity, fragment๊ฐ€ ํƒœ์–ด๋‚˜์„œ(์‹œ์ž‘๋˜๋‹ค) ์ฃฝ๋Š”(์ข…๋ฃŒ๋˜๋‹ค) ์ˆœ๊ฐ„๊นŒ์ง€์˜ ์ฃผ๊ธฐ๋ฅผ ๋œปํ•œ๋‹ค.

 


1. Activity์˜ ์ƒ๋ช…์ฃผ๊ธฐ(lifeCycle) 

๋จผ์ € activity์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ํ• ์•„๋ณผ ๊ฑด๋ฐ, activity์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋กœ๋Š”

  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroy()
  • onRestart()

์ด 7๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ฐ ํ•จ์ˆ˜๋“ค์€ activity์˜ ์ƒํƒœ์— ๋”ฐ๋ผ ํ˜ธ์ถœ์ด ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ๊ผญ ๋‹ค ๊ตฌํ˜„ํ•  ํ•„์š”๋Š” ์—†๋‹ค onCreate()๋Š” ์˜ˆ์™ธ์ง€๋งŒ, ์ž์‹ ์ด ๊ฐœ๋ฐœ ์ค‘์ธ ์•ฑ์˜ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•  ์ค„ ์•Œ์•„์•ผ ํ•œ๋‹ค.

 

์•กํ‹ฐ๋น„ํ‹ฐ ์ƒ๋ช…์ฃผ๊ธฐ ์ด๋ฏธ์ง€

 

1. onCreate()

onCreate๋Š” ์•ฑ์—์„œ activity๊ฐ€ ์‹คํ–‰ ๋ ๋•Œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜๋กœ, ๋ณ€์ˆ˜ ์ •์˜, xml์˜ ์ •์˜(ํ™”๋ฉด ๊ตฌ์„ฑ)๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

private lateinit var binding: ActivityMainBinding
private lateinit var textView: TextView
// onCreate(): activity๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ˆ˜ํ–‰๋œ๋‹ค(ํ•„์ˆ˜๋กœ ์žˆ์–ด์•ผ ํ•จ).
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    textView = binding.statusText
    textView.text = "activity created."
}

setContentView ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ lifeCycle ํ•จ์ˆ˜ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด onCreate() ์•ˆ์—์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

onCreate() ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ activity๋Š” "์‹œ์ž‘" ์ƒํƒœ๊ฐ€ ๋˜๋Š”๋ฐ, ์ด๋•Œ onStart()์™€ onResume()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

2. onStart()

onStart() ํ•จ์ˆ˜๋Š” activity๊ฐ€ "์‹œ์ž‘" ์ƒํƒœ์ผ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.

์ด๋•Œ "์‹œ์ž‘" ์ƒํƒœ๋Š” activity๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ์‹œ์ ์„ ๋œปํ•œ๋‹ค.

override fun onStart() {
    super.onStart()

    Log.d("TAG", "onStart: activity started.")
}

onStart()๊ฐ€ ์ˆ˜ํ–‰๋˜๊ณ  ๋‚œ ํ›„, activity๋Š” "Resume" ์ƒํƒœ๋กœ ์ง„์ž…ํ•˜๋ฉด์„œ onResume()๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค

 

 

3. onResume()

onResume() ํ•จ์ˆ˜๋Š” activity๊ฐ€ "Resume" ์ƒํƒœ์ผ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.

"Resume" ์ƒํƒœ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์•ฑ๊ณผ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋ฅผ ๋œปํ•œ๋‹ค.

override fun onResume() {
    super.onResume()

    Log.d("TAG", "onStart: activity resumed.")
}

 

 

onCreate() ์—์„œ onResume()๊นŒ์ง€์˜ ์•ฑ์˜ ๋ชจ์Šต

 

 

ํ•˜์ง€๋งŒ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ํ•ญ์ƒ "Resume" ์ƒํƒœ๋กœ๋งŒ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

intent๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ์—๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ "Paused" ์ƒํƒœ์— ๋“ค์–ด๊ฐ€ onPause()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

 

4. onPause()

onPause๋Š” activity๊ฐ€ "Paused" ์ƒํƒœ์ผ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.

"Paused" ์ƒํƒœ๋Š” acivity๊ฐ€ ์ผ์‹œ ์ •์ง€ ์ƒํƒœ๋ผ๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

override fun onPause() {
    super.onPause()

    Log.d("TAG", "onPause activity pause")
}

๋‹ค์‹œ activity๋ฅผ ์‹œ์ž‘ํ•˜๋ฉด, "Paused" ์ƒํƒœ๊ฐ€ ํ’€๋ฆฌ๊ณ  ๋‹ค์‹œ onResume()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

 

 

5. onStop

activity๊ฐ€ ํ™”๋ฉด์—์„œ ์™„์ „ํžˆ ์•ˆ๋ณด์ด๊ฒŒ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.

override fun onStop() {
    super.onStop()

    Log.d("TAG", "onPause activity stopped")
}

์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์‹œ ๋Œ์•„์˜ค๊ฒŒ ๋˜๋ฉด, "Stopped" ์ƒํƒœ์—์„œ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด์„œ onRestart(), onStart(), onResume() ์ˆœ์„œ๋Œ€๋กœ ๋‹ค์‹œ ํ˜ธ์ถœ๋œ๋‹ค.

 

 

6. onDestroy

activity๊ฐ€ ์™„์ „ํžˆ ์ฃฝ๊ธฐ(?) ์ „์— ์ˆ˜ํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

override fun onDestroy() {
    super.onDestroy()

    Log.d("TAG", "onPause activity died.")
}

 

 

onPause()๋ถ€ํ„ฐ onDestroy()๊นŒ์ง€์˜ ๋ชจ์Šต

 

 

 

์ •๋ฆฌ


์ด๋ฒˆ์— lifeCycle์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๊ฐ ํ•จ์ˆ˜๋“ค์˜ ์—ญํ• ์„ ๋” ์ •ํ™•ํžˆ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

๊ธฐ์กด์—๋Š” ๊ฒ€์ƒ‰์— ๊ธ‰๊ธ‰ํ•˜๋ฉด์„œ ์•ฑ์„ ๋งŒ๋“ค์—ˆ์œผ๋‚˜, ์ด์ œ๋Š” ์•ˆ ๊ทธ๋ž˜๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค~!

728x90