[Android] Activity

[Android] Activity

Activity

  • ๊ฐ๊ฐ์˜ activity๋Š” loosely bound(์˜์กด์„ฑ์ด ๋‚ฎ๋‹ค?)
  • activity๊ฐ€ ๋‹ค๋ฅธ app์— ์žˆ๋Š” activity๋ฅผ ์‹œ์ž‘์‹œํ‚ค๊ธฐ๋„ ํ•œ๋‹ค.
  • ์‚ฌ์šฉํ•˜๋ ค๋Š” activity๋Š” ๋ฉ”๋‹ˆํŽ˜์ŠคํŠธ์— ๋“ฑ๋กํ•ด์•ผํ•œ๋‹ค.

Life Cycle

Activity class๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ํ•ด์ฃผ๋Š” callback ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. (creating, stopping, resuming โ€ฆ)
callback ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ์ง€ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
life cycle callback์„ ์ž˜ ๊ตฌํ˜„ ํ•ด๋†“์œผ๋ฉด,

  • ๋‹ค๋ฅธ ์•ฑ์œผ๋กœ ์ „ํ™˜ํ•  ๋•Œ์˜ ์ถฉ๋Œ ๋ฐฉ์ง€
  • ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ์˜ ์‹œ์Šคํ…œ ์ž์› ๋‚ญ๋น„ ๋ฐฉ์ง€
  • ์•ฑ์„ ๋‹ค์‹œ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ด์ „ ์ƒํƒœ ์œ ์ง€ ๊ฐ€๋Šฅ
  • ํ™”๋ฉด ํšŒ์ „ ์‹œ crashing ๋ฐฉ์ง€, ์ƒํƒœ ์œ ์ง€ ๊ฐ€๋Šฅ

activity life cycle
[์ถœ์ฒ˜: https://developer.android.com/guide/components/activities/activity-lifecycle.html]

Life Cycle callbacks

์ฝœ๋ฐฑ ๋ฉ”์†Œ๋“œ ์„ค๋ช…
onCreate(savedInstanceState) - created state
- ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ
- savedInstanceState: ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ด์ „ ์ €์žฅ๋œ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. (์ด์ „์— ์‹คํ–‰ํ•œ ์ ์ด ์—†๋‹ค๋ฉด null)
onStart() - started state
- ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ํ™”๋ฉด์— ๋ณด์ด๊ธฐ ์ง์ „์— ํ˜ธ์ถœ
- ์‹คํ–‰์ด ๋๋‚˜๋ฉด Resumed state๋กœ ์ „ํ™˜
onResume() - resumed state
- ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ
- resumed state๋Š” interruptive ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „๊นŒ์ง€ ์œ ์ง€๋œ๋‹ค.
- paused state์—์„œ resumed state๋กœ ๋Œ์•„์˜ค๋ฉด, onResume()์ด ๋‹ค์‹œ ํ˜ธ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— paused state ๋™์•ˆ release์‹œ์ผฐ๋˜ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.
- onResume()์€ ์ฒ˜์Œ ์ƒ์„ฑ๋  ๋•Œ๋ฅผ ํฌํ•จํ•ด์„œ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ foreground๋กœ ์˜ฌ ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋œ๋‹ค.
onRestart() - ์•กํ‹ฐ๋น„ํ‹ฐ ์ค‘์ง€ ์ดํ›„ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋‹ค์‹œ ์‹œ์ž‘๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ
- ๋‹ค์Œ์—๋Š” ํ•ญ์ƒ onStart()
onPause() - ์‚ฌ์šฉ์ž๊ฐ€ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ๋– ๋‚ฌ์„ ๋•Œ(ํ•ญ์ƒ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์•„์˜ˆ ์†Œ๋ฉธ๋œ ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฑด ์•„๋‹˜) ํ˜ธ์ถœ
- ์‹œ์Šคํ…œ ์ž์›์„ releaseํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ฐฐํ„ฐ๋ฆฌ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” BR, GPS โ€ฆ)
- onPause()๋ฅผ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋‚˜ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ์ €์žฅ, db transacation ์‹คํ–‰ ๋“ฑ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋œ๋‹ค. -> ์ด๋Ÿฐ ์ž‘์—…์€ method๊ฐ€ ๋๋‚˜๊ธฐ ์ด์ „์— ์™„๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค.
- onPause()๊ฐ€ ๋๋‚ฌ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ”๋กœ ์ƒํƒœ๊ฐ€ ์ „ํ™˜๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ , ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ resume๋˜๊ฑฐ๋‚˜ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์™„์ „ํžˆ ์•ˆ ๋ณด์ผ ๋•Œ๊นŒ์ง€ ์œ ์ง€๋œ๋‹ค. -> ์™„์ „ํžˆ ์•ˆ ๋ณด์ด๋ฉด onStop()
onStop() - stopped state
- user๊ฐ€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋™์•ˆ ํ•„์š”์—†๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ์‹œ์Šคํ…œ ์ž์›์„ releaseํ•œ๋‹ค.
- stopped state์—์„œ ๋‹ค์‹œ user๊ฐ€ ์‚ฌ์šฉํ•˜๋ฉด onRestart() / ์•„์˜ˆ ์‹คํ–‰์„ ๋๋‚ด๋ฉด onDestroy()
- ์‹œ์Šคํ…œ์ด ๊ฐ•์ œ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ
onDestroy() - final call
- ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ destoryed ๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ
- ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ finish()๋ฅผ ํ†ตํ•ด ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์‹œ์Šคํ…œ์— ์˜ํ•ด ๊ฐ•์ œ ์ข…๋ฃŒ๋˜๋Š” ๊ฒฝ์šฐ์— ํ˜ธ์ถœ(isFinishing() ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌ๋ถ„)
- ์‹œ์Šคํ…œ์ด ๊ฐ•์ œ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ
์•กํ‹ฐ๋น„ํ‹ฐ ์‹คํ–‰๋  ๋•Œ callback ์ˆœ์„œ

onCreate โ†’ onStart โ†’ onResume โ†’ onPause โ†’ onStop โ†’ onDestroy

<์ฃผ์˜>
ํ™”๋ฉด ํšŒ์ „์„ ํ•˜๋ฉด ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์€ ๊ธฐ์กด ์•กํ‹ฐ๋น„ํ‹ฐ Destroy ~ ์ƒˆ๋กœ์šด ์•กํ‹ฐ๋น„ํ‹ฐ onCreate๋กœ ๋‹ค์‹œ ์‹œ์ž‘๋œ๋‹ค.
ํ™”๋ฉด ํšŒ์ „ ์‹œ ์•กํ‹ฐ๋น„ํ‹ฐ ์žฌ์‹œ์ž‘์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. -> activity ์ •๋ณด์— android:configChanges=โ€orientation|screenSizeโ€ ์ถ”๊ฐ€

<activity android:name=".MainActivity"
    android:configChanges="orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

=> ์ƒํƒœ์œ ์ง€์— ์œ ์˜ํ•  ๊ฒƒ.

์‹คํ–‰ ์ค‘์ธ ์•กํ‹ฐ๋น„ํ‹ฐ ๊ด€๋ฆฌ -> ์Šคํƒ

  • ์‹คํ–‰(active, running): ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ์ƒํƒœ
  • ์ •์ง€(stopped): ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ์— ์˜ํ•ด ์™„์ „ํžˆ ๊ฐ€๋ ค์ง„ ์ƒํƒœ / ์ •๋ณด ์œ ์ง€ - ์–ธ์ œ๋“  ๋‹ค์‹œ ํ™œ์„ฑํ™”๋  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ๊ฐ•์ œ์ข…๋ฃŒ
  • ์ผ์‹œ์ •์ง€(pause): ํฌ์ปค์Šค๋Š” ์žƒ์—ˆ์ง€๋งŒ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๋Š” ์ƒํƒœ, ์œ„์ชฝ์— ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์žˆ์ง€๋งŒ ํ™”๋ฉด์„ ์™„์ „ํžˆ ๊ฐ€๋ฆฌ์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ๋ฐ˜ํˆฌ๋ช…ํ•œ ๊ฒฝ์šฐ / ๊ฐ•์ œ์ข…๋ฃŒ ๊ฐ€๋Šฅ


[์ถœ์ฒ˜: https://developer.android.com/guide/components/activities/tasks-and-back-stack.html]



[์ฐธ๊ณ ]
์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ •๋ณต 1,2๊ถŒ
Do it! ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
Android Developers

Comments