Skip to content

IN-SOPT-ANDROID/youngjin-choi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

YoungJin

Week2 - Adapter ํŒจํ„ด์„ ํ™œ์šฉํ•˜๋Š” UI ์ปดํฌ๋„ŒํŠธ๋“ค


Fragment Lifecycle callbacks

  • Activity Lifecycle ๋น„๊ตํ•˜๋ฉด ์ƒ์„ฑ ์‹œ์—๋Š” onViewCreated() - onViewStateRestored()๊ฐ€ ์ถ”๊ฐ€๋กœ ์žˆ๊ณ , ํŒŒ๊ดด ์‹œ์—๋Š” onSaveInstanceState() - onDestroyView()๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
  • Fragment Lifecycle๋ณด๋‹ค View Lifecycle์ด ๋” ์งง๋‹ค.

Lifecycle.State enum

INITIALIZED, CREATED, STARTED, RESUMED, DESTROYED

onAttach()

  • ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋ถ™์„ ๋•Œ ํ˜ธ์ถœ
  • ์ธ์ž๋กœ Context๊ฐ€ ์ฃผ์–ด์ง

onCreate()

  • FragmentManager ์— add ๋์„ ๋•Œ ๋„๋‹ฌํ•˜๋ฉฐ onCreate() ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
  • ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ํ˜ธ์ถœ์„ ๋ฐ›์•„ ์ƒ์„ฑ
  • onCreate() ์ด์ „์— onAttach()๊ฐ€ ๋จผ์ € ํ˜ธ์ถœ๋จ
  • Bundle ํƒ€์ž…์œผ๋กœ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ(savedInstanceState)๊ฐ€ ๋„˜์–ด์˜ด. ์ด๋Š” onSaveInstanceState()์— ์˜์— ์ €์žฅ๋œ Bundle ๊ฐ’์ž„
  • ์—ฌ๊ธฐ์„œ ๋˜ ์•Œ์•„์•ผํ•  ๋ถ€๋ถ„์€ savedInstanceState ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ฒ˜์Œ ์ƒ์„ฑ ๋์„ ๋•Œ๋งŒ null ๋กœ ๋„˜์–ด์˜ค๋ฉฐ, onSaveInstanceState()๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š์•˜๋”๋ผ๋„ ๊ทธ ์ดํ›„ ์žฌ์ƒ์„ฑ๋ถ€ํ„ฐ๋Š” non-null๊ฐ’์œผ๋กœ ๋„˜์–ด์˜ด
  • Fragment View๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— Fragment์˜ View์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ๋‘๊ธฐ์— ์ ์ ˆํ•˜์ง€ ์•Š์Œ -> UI ์ดˆ๊ธฐํ™” ๋ถˆ๊ฐ€๋Šฅ

onCreateView()

  • ๋ ˆ์ด์•„์›ƒ inflate ๋‹ด๋‹น
  • onCreateView()์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์ •์ƒ์ ์ธ Fragment View๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ–ˆ์„ ๋•Œ๋งŒ Fragment View์˜ Lifecycle์ด ์ƒ์„ฑ๋จ
  • savedInstanceState๋กœ ์ด์ „ ์ƒํƒœ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์ œ๊ณต
  • View์™€ ๊ด€๋ จ๋œ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ์Œ

onViewCreated()

  • onCreateView()๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜๋œ View ๊ฐ์ฒด๋Š” onViewCreated()์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋จ
  • ์ด ์‹œ์ ๋ถ€ํ„ฐ๋Š” Fragment View์˜ Lifecycle์ด INITIALIZED ์ƒํƒœ๋กœ ์—…๋ฐ์ดํŠธ ๋์Œ
  • ๋•Œ๋ฌธ์— View์˜ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •, LiveData ์˜ต์ €๋น™, RecyclerView, ViewPager2์— ์‚ฌ์šฉ๋  Adapter ์„ธํŒ… ์ ์ ˆ

onViewStateRestored()

  • ํ•จ์ˆ˜๋Š” ์ €์žฅํ•ด๋‘” ๋ชจ๋“  state ๊ฐ’์ด Fragment์˜ View ๊ณ„์ธต๊ตฌ์กฐ์— ๋ณต์› ๋์„ ๋•Œ ํ˜ธ์ถœ๋จ
  • ๋”ฐ๋ผ์„œ ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์ฒดํฌ๋ฐ•์Šค ์œ„์ ฏ์ด ํ˜„์žฌ ์ฒดํฌ ๋˜์–ด์žˆ๋Š”์ง€ ๋“ฑ ๊ฐ ๋ทฐ์˜ ์ƒํƒœ๊ฐ’์„ ์ฒดํฌํ•  ์ˆ˜ ์žˆ์Œ

onStart()

  • Fragment๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์งˆ ์ˆ˜ ์žˆ์„ ๋•Œ ํ˜ธ์ถœ๋จ
  • ์ฃผ๋กœ Fragment๊ฐ€ attach ๋˜์–ด์žˆ๋Š” Activity์˜ onStart() ์‹œ์ ๊ณผ ์œ ์‚ฌ
  • ์ด ์‹œ์ ๋ถ€ํ„ฐ๋Š” Fragment์˜ child FragmentManager ํ†ตํ•ด FragmentTransaction์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ
  • Fragment์˜ Lifecycle์ด STARTED๋กœ ์ด๋™ํ•œ ํ›„์— Fragment View์˜ Lifecycle ๋˜ํ•œ STARTED๋กœ ๋ณ€ํ™˜

onResume()

  • ์‚ฌ์šฉ์ž์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์ผ ๋•Œ ํ˜ธ์ถœ. ๋‹ค์‹œ ๋งํ•ด onResume()์ด ํ˜ธ์ถœ๋˜์ง€ ์•Š์€ ์‹œ์ ์—์„œ๋Š” ์ž…๋ ฅ์„ ์‹œ๋„ํ•˜๊ฑฐ๋‚˜ ํฌ์ปค์Šค๋ฅผ ์„ค์ •ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ž„์˜๋กœ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธ
  • Fragment๊ฐ€ ๋ณด์ด๋Š” ์ƒํƒœ์—์„œ ๋ชจ๋“  Animator์™€ Transition ํšจ๊ณผ๊ฐ€ ์ข…๋ฃŒ๋˜๊ณ , ํ”„๋ž˜๊ทธ๋จผํŠธ์™€ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ onResume() ํ˜ธ์ถœ๋จ
  • onStart()์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฃผ๋กœ Activity์˜ onResume()์‹œ์ ๊ณผ ์œ ์‚ฌ

onPause()

  • ์‚ฌ์šฉ์ž๊ฐ€ Fragment ๋ฅผ ๋– ๋‚˜๊ธฐ ์‹œ์ž‘ํ–ˆ์ง€๋งŒ Fragment๊ฐ€ visible ์ผ ๋•Œ onPause()๊ฐ€ ํ˜ธ์ถœ
  • ์ด ๋•Œ Faragment์™€ View์˜ Lifecycle์ด PAUSED๊ฐ€ ์•„๋‹Œ STARTED๊ฐ€ ๋จ // Lifecycle์— PAUSE์™€ STOP์— ํ•ด๋‹นํ•˜๋Š” ์ƒํƒœ๊ฐ€ ์—†์Œ

onStop()

  • Fragment๊ฐ€ ๋” ์ด์ƒ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€์ง€ ์•Š๊ฒŒ ๋˜๋ฉด onStop() ์ฝœ๋ฐฑ ํ˜ธ์ถœ
  • ๋ถ€๋ชจ ์•กํ‹ฐ๋น„ํ‹ฐ, ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ค‘๋‹จ๋  ๋•Œ, ์ƒํƒœ๊ฐ€ ์ €์žฅ๋  ๋•Œ ํ˜ธ์ถœ
  • View์™€ Lifecycle: STARTED โ†’ CREATED
  • API 28๋ฒ„์ „์„ ๊ธฐ์ ์œผ๋กœ onSaveInstanceState() ํ•จ์ˆ˜์™€ onStop() ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ์ง, ๋”ฐ๋ผ์„œ onStop()์ด FragmentTransaction์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์ง€์ ์ด ๋จ

onDestoryView()

  • ๋ชจ๋“  exit animation, transaction์ด ์™„๋ฃŒ๋˜๊ณ  Fragment๊ฐ€ ํ™”๋ฉด์œผ๋กœ๋ถ€ํ„ฐ ๋ฒ—์–ด๋‚ฌ์„ ๊ฒฝ์šฐ ํ˜ธ์ถœ
  • view์™€ lifecycle: CREATED โ†’ DESTROYED
  • getViewLifecycleOwnerLiveData()์˜ ๋ฆฌํ„ด๊ฐ’์œผ๋กœ null ์ด ๋ฐ˜ํ™˜
  • ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ์˜ํ•ด ์ˆ˜๊ฑฐ๋  ์ˆ˜ ์žˆ๋„๋ก Fragment View์— ๋Œ€ํ•œ ๋ชจ๋“  ์ฐธ์กฐ๊ฐ€ ์ œ๊ฑฐ๋˜์–ด์•ผ ํ•จ

onDestroy()

  • Fragment๊ฐ€ ์ œ๊ฑฐ๋˜๊ฑฐ๋‚˜, FragmentManager๊ฐ€ destroy ๋์„ ๊ฒฝ์šฐ, ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ Lifecycle ์€ DESTROYED ์ƒํƒœ๊ฐ€ ๋˜๊ณ , onDestroy() ํ˜ธ์ถœ
  • Fragment Lifecycle์˜ ๋์„ ์•Œ๋ฆผ
  • onAttach() ๊ฐ€ onCreate()์ด์ „์— ํ˜ธ์ถœ ๋๋˜ ๊ฒƒ์ฒ˜๋Ÿผ onDetach()๋˜ํ•œ onDestroy()์ดํ›„ ํ˜ธ์ถœ๋จ

onDetach()

  • ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ๋ถ€ํ„ฐ ํ•ด์ œ๋˜์–ด์งˆ ๋•Œ ํ˜ธ์ถœ๋จ

week1

Week1 - View, ViewGroup๊ณผ UI ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ


Activity Lifecycle callbacks

Activity ํด๋ž˜์Šค๋Š” Activity ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์ฝœ๋ฐฑ์„ ์ œ๊ณตํ•œ๋‹ค. Activity ํด๋ž˜์Šค๋Š” ์ˆ˜๋ช… ์ฃผ๊ธฐ์˜ ๋‹จ๊ณ„ ๊ฐ„ ์ „ํ™˜์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด 6๊ฐ€์ง€ ์ฝœ๋ฐฑ onCreate(), onStart(), onResume(), onPause(), onStop()๋ฐ onDestroy()๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์‹œ์Šคํ…œ์€ Activity์ด ์ƒˆ๋กœ์šด ์ƒํƒœ์— ๋“ค์–ด๊ฐˆ ๋•Œ ๊ฐ ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•œ๋‹ค.


onCreate()

  • ์‹œ์Šคํ…œ์ด ์ฒ˜์Œ Activity๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ์ด๋‹ค.

  • Activity ์ƒ์„ฑ ์‹œ Activity๋Š” Created ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

  • onCreate()์—์„œ๋Š” Activity์˜ ์ „์ฒด ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋™์•ˆ ํ•œ ๋ฒˆ๋งŒ ๋ฐœ์ƒํ•ด์•ผ ํ•˜๋Š” ๊ธฐ๋ณธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์˜ˆ์‹œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    • Databinding ์ˆ˜ํ–‰
    • Activity๋ฅผ ViewModel๊ณผ ์—ฐ๊ฒฐ
    • ์ผ๋ถ€ ํด๋ž˜์Šค ๋ฒ”์œ„ ๋ณ€์ˆ˜๋ฅผ ์ธ์Šคํ„ด์Šคํ™”
  • ์ด ๋ฉ”์†Œ๋“œ๋Š” Activity์˜ ์ด์ „ ์ƒํƒœ๋ฅผ ํฌํ•จํ•˜๋Š” Bundle ๊ฐ์ฒด์ธ ๋งค๊ฐœ ๋ณ€์ˆ˜ InstanceState๋ฅผ ์ˆ˜์‹ ํ•œ๋‹ค. ์ด์ „์— Activity๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ Bundle ๊ฐ์ฒด ๊ฐ’์€ null์ด๋‹ค.

  • onCreate() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰์„ ๋งˆ์น˜๋ฉด ์ž‘์—…์ด Started ์ƒํƒœ๋กœ ์ „ํ™˜๋˜๊ณ , ์‹œ์Šคํ…œ์€ onStart() ๋ฉ”์„œ๋“œ์™€ onResume() ๋ฉ”์„œ๋“œ๋ฅผ ๋น ๋ฅธ ์—ฐ์†์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค.

lateinit var textView: TextView
var gameState: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // ์ด์ „ ์ƒํƒœ๊ฐ’ ๋ณต์›
    gameState = savedInstanceState?.getString(GAME_STATE_KEY)

    // ํ™”๋ฉด Layout ์ •์˜ ๋ฐ ๋ทฐ ์ƒ์„ฑ
    setContentView(R.layout.main_activity)

    // ํ…์ŠคํŠธ ๋ทฐ ์ดˆ๊ธฐํ™”
    textView = findViewById(R.id.text_view)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
    textView.text = savedInstanceState?.getString(TEXT_VIEW_KEY)
}

override fun onSaveInstanceState(outState: Bundle?) {
    outState?.run {
        putString(GAME_STATE_KEY, gameState)
        putString(TEXT_VIEW_KEY, textView.text.toString())
    }
    super.onSaveInstanceState(outState)
}

onStart()

  • Activity๊ฐ€ Started ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์‹œ์Šคํ…œ์ด ์ด onStart()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  • ์•ฑ ์ด ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ์ง„์ž…ํ•˜์—ฌ ์ƒํ˜ธ์ž‘์šฉ์ด ๋˜๋„๋ก ์ค€๋น„ํ•˜์—ฌ Activity๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๋„๋ก ํ•œ๋‹ค.

  • ์ด ๋ฉ”์„œ๋“œ๋Š” ์•ฑ์ด UI๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ณณ์ด๋‹ค.

  • onStart()๋Š” ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์™„๋ฃŒ๋˜๋ฉฐ Created ์ƒํƒœ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Activity๊ฐ€ Started ์ƒํƒœ๋กœ ์œ ์ง€๋˜์ง€ ์•Š๋Š”๋‹ค.

  • ์ด ์ฝœ๋ฐฑ์ด ์™„๋ฃŒ๋˜๋ฉด Activity๊ฐ€ Resumed ์ƒํƒœ๊ฐ€ ๋˜๊ณ  ์‹œ์Šคํ…œ์€ onResume()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

onResume()

  • Activity๊ฐ€ Resumed ์ƒํƒœ๋กœ ์ „ํ™˜๋˜๋ฉด ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ์ด๋™ํ•œ ๋‹ค์Œ ์‹œ์Šคํ…œ์ด onResume() ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•œ๋‹ค.

  • ์•ฑ์ด ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ์ƒํƒœ์ด๋‹ค.

  • ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์•ฑ์—์„œ ํฌ์ปค์Šค๊ฐ€ ๋– ๋‚  ๋•Œ๊นŒ์ง€๋Š” ์•ฑ์ด ์ด ์ƒํƒœ์—์„œ ๋จธ๋ฌด๋ฅด๊ฒŒ ๋œ๋‹ค.

    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์ „ํ™”๋ฅผ ๋ฐ›๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค๋ฅธ Activity๋ฅผ ํƒ์ƒ‰ํ•˜๊ฑฐ๋‚˜ ๋””๋ฐ”์ด์Šค ํ™”๋ฉด์ด ๊บผ์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ์ด๋‹ค.
  • ์ค‘๋‹จ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ž‘์—…์ด Paused ์ƒํƒœ๋กœ ์ „ํ™˜๋˜๊ณ  ์‹œ์Šคํ…œ์ด onPause() ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•œ๋‹ค.

  • Paused ์ƒํƒœ์—์„œ Resumed ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€๋ฉด ์‹œ์Šคํ…œ์ด ๋‹ค์‹œ ํ•œ๋ฒˆ onResume()์„ ํ˜ธ์ถœํ•œ๋‹ค.

  • onResume()์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋„๋ก ๊ตฌํ˜„ํ–ˆ์œผ๋ฉด onPause() ์ค‘์— ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•œ๋‹ค.

    • ON_START <-> ON_STOP : ON_START ์ด๋ฒคํŠธ ์ดํ›„์— ์ดˆ๊ธฐํ™”๋ฅผ ํ–ˆ๋‹ค๋ฉด ON_STOP ์ด๋ฒคํŠธ ์ดํ›„์— ํ•ด์ œํ•˜๊ฑฐ๋‚˜ ์ข…๋ฃŒํ•œ๋‹ค.
    • ON_RESUME <-> ON_PAUSE : ON_RESUME ์ด๋ฒคํŠธ ์ดํ›„์— ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒฝ์šฐ ON_PAUSE ์ด๋ฒคํŠธ ์ดํ›„์— ํ•ด์ œํ•œ๋‹ค.
// ์ˆ˜๋ช…์ฃผ๊ธฐ๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ON_RESUME ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•  ๋•Œ ์นด๋ฉ”๋ผ์— ์•ก์„ธ์Šคํ•˜๋Š” ์˜ˆ์‹œ
class CameraComponent : LifecycleObserver {

    ...

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun initializeCamera() {
        if (camera == null) {
            getCamera()
        }
    }

    ...
}

onPause()

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

  • Activity๊ฐ€ Paused ์ƒํƒœ๋กœ ์ „ํ™˜๋˜๋Š” ๋ฐ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ด์œ ๊ฐ€ ์žˆ๋‹ค.

    • ๊ฐ€์žฅ ํ”ํ•œ ์ผ€์ด์Šค๋Š” ์ผ๋ถ€ ์ด๋ฒคํŠธ๋กœ ์•ฑ ์‹คํ–‰์ด ์ค‘๋‹จ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
    • Android 7.0(API ๋ ˆ๋ฒจ 24) ์ด์ƒ์—์„œ๋Š” ์—ฌ๋Ÿฌ ์•ฑ์ด ๋ฉ€ํ‹ฐ ์œˆ๋„์šฐ ๋ชจ๋“œ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•ญ์ƒ ํ•˜๋‚˜์˜ ์•ฑ(์œˆ๋„์šฐ)์—๋งŒ ํฌ์ปค์Šค๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์Šคํ…œ์€ ๋‹ค๋ฅธ ๋ชจ๋“  ์•ฑ์„ ์ผ์‹œ ์ค‘์ง€ํ•œ๋‹ค.
    • ๋‹ค์ด์–ผ๋กœ๊ทธ ๊ฐ™์€ ์ƒˆ๋กœ์šด ๋ฐ˜ํˆฌ๋ช… Activity๊ฐ€ ์—ด๋ฆด ๋•Œ์ด๋‹ค. Activity๊ฐ€ ์—ฌ์ „ํžˆ ๋ถ€๋ถ„์ ์œผ๋กœ ๋ณด์ด์ง€๋งŒ ์ดˆ์ ์ด ๋งž์ง€ ์•Š๋Š” ํ•œ Paused ์ƒํƒœ๋กœ ๋‚จ์•„ ์žˆ๋‹ค.
  • onPause()์—์„œ ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค, ์„ผ์„œ(์˜ˆ: GPS)์— ๋Œ€ํ•œ ํ•ธ๋“ค ๋˜๋Š” ์ž‘์—…์ด ์ผ์‹œ ์ค‘์ง€๋˜๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋™์•ˆ ๋ฐฐํ„ฐ๋ฆฌ ์ˆ˜๋ช…์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฉ€ํ‹ฐ ์œˆ๋„์šฐ ๋ชจ๋“œ์ธ ๊ฒฝ์šฐ Paused ์ƒํƒœ์ธ Activity๊ฐ€ ์—ฌ์ „ํžˆ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ UI ๊ด€๋ จ ๋ฆฌ์†Œ์Šค๋ฅผ ์™„์ „ํžˆ ํ•ด์ œํ•˜๊ฑฐ๋‚˜ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์„ ๋ฉ€ํ‹ฐ ์œˆ๋„์šฐ ๋ชจ๋“œ๋ฅผ ๋” ์ž˜ ์ง€์›ํ•˜๋ ค๋ฉด onPause() ๋Œ€์‹  onStop()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์•ผํ•œ๋‹ค.

  • onPause()์‹คํ–‰์€ ๋งค์šฐ ์งง์œผ๋ฉฐ ์ €์žฅ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•œ ์‹œ๊ฐ„์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๋˜๋Š” ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ ํ˜ธ์ถœ์„ ํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜์„ ์‹คํ–‰ํ•  ๋•Œ onPause()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. OnStop()์ค‘์— ๊ณผ๋ถ€ํ•˜ ์ข…๋ฃŒ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

  • onPause() ๋ฉ”์„œ๋“œ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๊ณ  ์ž‘์—…์ด ์ผ์‹œ ์ค‘์ง€๋œ ์ƒํƒœ๊ฐ€ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋Œ€์‹  Activity๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์™„์ „ํžˆ ๋ณผ ์ˆ˜ ์—†๊ฒŒ ๋  ๋•Œ๊นŒ์ง€ Activity๋Š” ์ด ์ƒํƒœ๋กœ ์œ ์ง€๋œ๋‹ค.

// ON_RESUME ์ด๋ฒคํŠธ๊ฐ€ ์ˆ˜์‹ ๋œ ํ›„ ์ดˆ๊ธฐํ™”๋œ ์นด๋ฉ”๋ผ๋ฅผ ํ•ด์ œํ•œ๋‹ค.
class CameraComponent : LifecycleObserver {
    ...
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun releaseCamera() {
        camera?.release()
        camera = null
    }
    ...
}

onStop()

  • Activity๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์™„์ „ํžˆ ๋ณด์ด์ง€ ์•Š๊ฒŒ ๋˜๋ฉด Stopped ์ƒํƒœ์— ๋“ค์–ด๊ฐ€๊ณ , ์‹œ์Šคํ…œ์€ onStop() ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•œ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด ์ƒˆ๋กœ ์‹œ์ž‘๋œ Activity๊ฐ€ ํ™”๋ฉด ์ „์ฒด๋ฅผ ์ฐจ์ง€ํ•  ๊ฒฝ์šฐ์— ์ ์šฉ๋œ๋‹ค.

  • ์‹œ์Šคํ…œ์€ Activity์˜ ์‹คํ–‰์ด ์™„๋ฃŒ๋˜์–ด ์ข…๋ฃŒ๋  ์‹œ์ ์— onStop()์„ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

  • onStop()์—์„œ๋Š” ์•ฑ์ด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด์ง€ ์•Š๋Š” ๋™์•ˆ ์•ฑ์€ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•˜๊ฑฐ๋‚˜ ์กฐ์ •ํ•ด์•ผํ•œ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด ์•ฑ์€ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ผ์‹œ์ค‘์ง€ํ•˜๊ฑฐ๋‚˜, ์„ธ๋ฐ€ํ•œ ์œ„์น˜ ์—…๋ฐ์ดํŠธ์—์„œ ๋Œ€๋žต์ ์ธ ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • onPause() ๋Œ€์‹  onStop()์„ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ€ํ‹ฐ ์œˆ๋„์šฐ ๋ชจ๋“œ์—์„œ ํ™œ๋™์„ ๋ณด๊ณ  ์žˆ๋”๋ผ๋„ UI ๊ด€๋ จ ์ž‘์—…์ด ๊ณ„์† ์ง„ํ–‰๋œ๋‹ค.

  • ๋˜ํ•œ onStop()์„ ์‚ฌ์šฉํ•˜์—ฌ CPU๋ฅผ ๋น„๊ต์  ๋งŽ์ด ์†Œ๋ชจํ•˜๋Š” ์ข…๋ฃŒ ์ž‘์—…์„ ์‹คํ–‰ํ•ด์•ผํ•œ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด ์ •๋ณด๋ฅผ DB์— ์ €์žฅํ•  ์ ์ ˆํ•œ ์‹œ๊ธฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด onStop() ์ƒํƒœ์ผ ๋•Œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Activitiy๊ฐ€ Stopped ์ƒํƒœ์— ๋“ค์–ด๊ฐ€๋ฉด Activity ๊ฐ์ฒด๋Š” ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์— ๋จธ๋ฌด๋ฅด๊ฒŒ ๋œ๋‹ค. ์ด ๊ฐ์ฒด๊ฐ€ ๋ชจ๋“  ์ƒํƒœ ๋ฐ ๋ฉค๋ฒ„ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜์ง€๋งŒ window ๋งค๋‹ˆ์ €์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€๋Š” ์•Š๋‹ค.

  • Activity๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘๋˜๋ฉด ์ด ์ •๋ณด๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•œ๋‹ค. ์ตœ์ƒ์œ„ ์ƒํƒœ๊ฐ€ Resumed ์ƒํƒœ์ธ ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ ์ค‘์— ์ƒ์„ฑ๋œ ๊ตฌ์„ฑ์š”์†Œ๋Š” ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

  • ๋˜ํ•œ ์‹œ์Šคํ…œ์€ ๋ ˆ์ด์•„์›ƒ์— ์žˆ๋Š” ๊ฐ View ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ๋„ ๊ธฐ๋กํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž๊ฐ€ EditText ์œ„์ ฏ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ๋‚ด์šฉ์ด ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ €์žฅ ๋ฐ ๋ณต์›ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

  • Activity๋Š” Stopped ์ƒํƒœ์—์„œ ๋‹ค์‹œ ์‹œ์ž‘๋˜์–ด ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ฑฐ๋‚˜, ์‹คํ–‰์„ ์ข…๋ฃŒํ•˜๊ณ  ์‚ฌ๋ผ์ง„๋‹ค.

  • ํ™œ๋™์ด ๋‹ค์‹œ ์‹œ์ž‘๋˜๋ฉด ์‹œ์Šคํ…œ์€ onRestart()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

// ์ดˆ์•ˆ ๋‚ด์šฉ์„ ๋กœ์ปฌ DB์— ์ €์žฅํ•˜๋Š” onStop()์„ ๊ตฌํ˜„ํ•œ ์˜ˆ์ œ
override fun onStop() {
    super.onStop()

    val values = ContentValues().apply {
        put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText())
        put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle())
    }

    asyncQueryHandler.startUpdate(
            token,
            null,     
            uri,    
            values,   
            null,
            null     
    )
}

onDestroy()

  • Activity๊ฐ€ ์‹คํ–‰์„ ์ข…๋ฃŒํ•˜๋ฉด ์‹œ์Šคํ…œ์€ onDestroy()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  • ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ์ด ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•œ๋‹ค.

    • Activitiy ์ข…๋ฃŒ
    • configuration change(์˜ˆ: ์žฅ์น˜ ํšŒ์ „ ๋˜๋Š” ๋ฉ€ํ‹ฐ ์œˆ๋„์šฐ ๋ชจ๋“œ)์œผ๋กœ ์ธํ•ด ์‹œ์Šคํ…œ์ด ์ผ์‹œ์ ์œผ๋กœ Activitiy๋ฅผ ์ค‘๋‹จ
  • Activity๊ฐ€ configuration chnage์œผ๋กœ ์ธํ•ด ๋‹ค์‹œ ์ƒ์„ฑ๋  ๊ฒฝ์šฐ ViewModel์€ ๊ทธ๋Œ€๋กœ ๋ณด์กด๋˜์–ด ๋‹ค์Œ Activity ์ธ์Šคํ„ด์Šค์— ์ „๋‹ฌ๋˜๋ฏ€๋กœ ์ถ”๊ฐ€ ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. Activity๊ฐ€ ๋‹ค์‹œ ์ƒ์„ฑ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ViewModel์€ onCleared()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Activity๊ฐ€ ์†Œ๋ฉธ๋˜๊ธฐ ์ „์— ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

  • onDestroy()๋Š” Activity๊ฐ€ ์ˆ˜์‹ ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์ˆ˜๋ช… ์ฃผ๊ธฐ ์ฝœ๋ฐฑ์ด ๋œ๋‹ค.

  • onDestroy() ์ฝœ๋ฐฑ์€ ์ด์ „์˜ ์ฝœ๋ฐฑ(์˜ˆ: onStop())์—์„œ ์•„์ง ํ•ด์ œ๋˜์ง€ ์•Š์€ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•ด์•ผ ํ•œ๋‹ค.

Summary

Method Activity State Description
onCreate() ๋งŒ๋“ค์–ด์ง Activity ์ƒ์„ฑํ•  ๋•Œ
onStart() ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚จ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ
onResume() ํ˜„์žฌ ์‹คํ–‰ ์ค‘ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚จ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜ ์žˆ๊ณ  ์‹คํ–‰์ค‘์ผ ๋•Œ
onPause() ํ™”๋ฉด์ด ๊ฐ€๋ ค์ง Activityํ™”๋ฉด์˜ ์ผ๋ถ€๊ฐ€ ๋‹ค๋ฅธ Activity์— ๊ฐ€๋ ค์ง
onStop() ํ™”๋ฉด์ด ์—†์–ด์ง ๋‹ค๋ฅธ Activity์˜ ์‹คํ–‰์œผ๋กœ ์™„์ „ํžˆ ๊ฐ€๋ ค์ง
onDestroy() ์ข…๋ฃŒ๋จ Activity ์ข…๋ฃŒ๋จ

Reference

Activity ์ƒ๋ช…์ฃผ๊ธฐ ํ‘œ ์ถœ์ฒ˜ Activity Lifecycle ๊ณต์‹๋ฌธ์„œ



Rules

This is your repository for 31st SOPT Android Development Seminar and Assignment

๋ ˆํฌ์ง€ํ„ฐ๋ฆฌ์—๋Š” ์ด 3๊ฐœ์˜ ๋ธŒ๋žœ์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • master
    • ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ๋ธŒ๋žœ์น˜์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ์—ฐ์Šต ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ ์ž ํ• ๋•Œ ์ด ๋ธŒ๋žœ์น˜์—์„œ ์ƒˆ๋กœ์šด ๋ธŒ๋žœ์น˜๋ฅผ ํŒŒ์…”์„œ ๋งŒ๋“œ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • develop/view
    • ์—ฌ๋Ÿฌ๋ถ„์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ณผ์ œ๋Š” ์ด ๋ธŒ๋žœ์น˜์—์„œ ์ด๋ค„์ง€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
    • develop/view ๋ธŒ๋žœ์น˜์—์„œ ์ƒˆ๋กœ์šด feature ๋ธŒ๋žœ์น˜๋ฅผ ํŒŒ๊ณ  ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ
    • ๊ณผ์ œ๋ฅผ ์™„๋ฃŒํ•˜๋ฉด ํ•ด๋‹น ๊ณผ์ œ๋ฅผ develop/view์— PR์„ ์˜ฌ๋ ค์ฃผ์‹œ๊ณ  ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
    • ๋จธ์ง€๊นŒ์ง€ ์™„๋ฃŒํ•˜์‹œ๋ฉด ๊ณผ์ œ ์™„๋ฃŒ๋กœ ์ธ์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • develop/compose
    • ์ด๋ฒˆ ๊ธฐ์ˆ˜ ์‹ฌํ™”๊ณผ์ œ์ธ Compose ๊ณผ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋ธŒ๋žœ์น˜์ž…๋‹ˆ๋‹ค.
    • develop/compose ๋ธŒ๋žœ์น˜์—์„œ ์‹ฌํ™”๊ณผ์ œ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์˜ ์ž‘์—…๋“ค์„ ์ง„ํ–‰ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
    • ๊ธฐ์กด view ๋ธŒ๋žœ์น˜๋“ค๊ณผ๋Š” ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ๊ณผ์ œ๋ฅผ ์™„๋ฃŒํ•˜๋ฉด ํ•ด๋‹น ๊ณผ์ œ๋ฅผ develop/compose์— PR์„ ์˜ฌ๋ ค์ฃผ์‹œ๊ณ  ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
    • Compose ๊ฐœ๋ฐœํ™˜๊ฒฝ์€
      • Kotlin: 1.7.0
      • Compose: 1.2.1
    • ์œผ๋กœ ๋งž์ถฐ๋†“๊ฒ ์Šต๋‹ˆ๋‹ค.

About

๐Ÿ‘ ๐Ÿฐ ๐Ÿ‘

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages