[AAC] Lifecycles

dwjeongยท2023๋…„ 11์›” 7์ผ
0

์•ˆ๋“œ๋กœ์ด๋“œ

๋ชฉ๋ก ๋ณด๊ธฐ
13/28
post-custom-banner

๐Ÿ”Ž Lifecycle-Aware Components๋กœ ์ˆ˜๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ


์ผ๋ฐ˜์ ์œผ๋กœ ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ์ข…์† ์ปดํฌ๋„ŒํŠธ์˜ ์ž‘์—…์„ ๊ตฌํ˜„ํ•จ.

๐Ÿ‘‰ ์ด๋Ÿฌํ•œ ํŒจํ„ด์€ ์ฝ”๋“œ์˜ ๊ตฌ์„ฑ์„ ๋‚˜์˜๊ฒŒํ•˜๊ณ  ์˜ค๋ฅ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์›์ธ์ด ๋จ.

Lifecycle-aware ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ข…์† ์ปดํฌ๋„ŒํŠธ์˜ ์ฝ”๋“œ๋ฅผ ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ๋ฐ–์œผ๋กœ ์ด๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ.
Lifecycle-aware ์ปดํฌ๋„ŒํŠธ๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ ๋ฐ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด ์ƒํƒœ ๋ณ€ํ™”์— ๋”ฐ๋ผ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•จ. ์ด๋Ÿฌํ•œ ์ปดํฌ๋„ŒํŠธ๋“ค์€ ๋” ๊ฐ€๋ณ๊ณ  ์กฐ์งํ™”๋œ ์ฝ”๋“œ๋ฅผ ์ƒ์‚ฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ณ  ๊ด€๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด์คŒ.


internal class MyLocationListener(
        private val context: Context,
        private val callback: (Location) -> Unit
) {

    fun start() {
        // connect to system location service
    }

    fun stop() {
        // disconnect from system location service
    }
}

class MyActivity : AppCompatActivity() {
    private lateinit var myLocationListener: MyLocationListener

    override fun onCreate(...) {
        myLocationListener = MyLocationListener(this) { location ->
            // update UI
        }
    }

    public override fun onStart() {
        super.onStart()
        myLocationListener.start()
        // manage other components that need to respond
        // to the activity lifecycle
    }

    public override fun onStop() {
        super.onStop()
        myLocationListener.stop()
        // manage other components that need to respond
        // to the activity lifecycle
    }
}

์œ„์˜ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์–ด๋ณด์ด์ง€๋งŒ ๋ผ์ดํ”„์‚ฌ์ดํด์˜ ํ˜„์žฌ ์ƒํƒœ์— ๋”ฐ๋ผ UI ๋ฐ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํ˜ธ์ถœ์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ.

์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ด€๋ฆฌํ•  ๊ฒฝ์šฐ onStart() ๋ฐ onStop() ๊ณผ ๊ฐ™์€ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ์— ์ƒ๋‹นํ•œ ์–‘์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์–ด ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์ง.

์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•กํ‹ฐ๋น„ํ‹ฐ๋‚˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ค‘์ง€๋˜๊ธฐ ์ „์— ์‹œ์ž‘๋œ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†์Œ. (์˜ˆ๋ฅผ ๋“ค์–ด onStart()์—์„œ ์„ค์ • ํ™•์ธ๊ณผ ๊ฐ™์€ ๊ธด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ)

onStop() ๋ฉ”์„œ๋“œ๊ฐ€ onStart() ๋ฉ”์„œ๋“œ๋ณด๋‹ค ๋จผ์ € ์™„๋ฃŒ๋˜์–ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•„์š” ์ด์ƒ์œผ๋กœ ์˜ค๋ž˜ ์œ ์ง€๋˜๋Š” ๊ฒฝ์Ÿ์ƒํƒœ(race condition)๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋จ.



๊ฐœ์„ ๋œ ์ฝ”๋“œ

class MyActivity : AppCompatActivity() {
    private lateinit var myLocationListener: MyLocationListener

    override fun onCreate(...) {
        myLocationListener = MyLocationListener(this) { location ->
            // update UI
        }
    }

    public override fun onStart() {
        super.onStart()
        Util.checkUserStatus { result ->
            // what if this callback is invoked AFTER activity is stopped?
            if (result) {
                myLocationListener.start()
            }
        }
    }

    public override fun onStop() {
        super.onStop()
        myLocationListener.stop()
    }

}



๐Ÿ“– Lifecycle

๋ผ์ดํ”„ ์‚ฌ์ดํด์€ ์•กํ‹ฐ๋น„ํ‹ฐ๋‚˜ ํ”„๋ž˜๊ทธ๋จผํŠธ์™€ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ์ƒํƒœ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณด์œ ํ•˜๊ณ  ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์ด ์ด ์ƒํƒœ๋ฅผ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค.

  • Event
    ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋ผ์ดํ”„์‚ฌ์ดํด ํด๋ž˜์Šค์—์„œ ์ „๋‹ฌ๋˜๋Š” ๋ผ์ดํ”„์‚ฌ์ดํด ์ด๋ฒคํŠธ. ์ด๋ฒคํŠธ๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ์ฝœ๋ฐฑ ์ด๋ฒคํŠธ์™€ ๋งคํ•‘๋จ.

  • State
    ๋ผ์ดํ”„์‚ฌ์ดํด ๊ฐ์ฒด์— ์˜ํ•ด ์ถ”์ ๋˜๋Š” ๊ตฌ์„ฑ์š”์†Œ์˜ ํ˜„์žฌ ์ƒํƒœ.


์œ„ ๊ทธ๋ฆผ์—์„œ State๋Š” ๊ทธ๋ž˜ํ”„์˜ ๋…ธ๋“œ, Event๋Š” ๋…ธ๋“œ ์‚ฌ์ด์˜ ์—ฃ์ง€.



class MyObserver : DefaultLifecycleObserver {
    override fun onResume(owner: LifecycleOwner) {
        connect()
    }

    override fun onPause(owner: LifecycleOwner) {
        disconnect()
    }
}

myLifecycleOwner.getLifecycle().addObserver(MyObserver())

DefaultLifecycleObserver๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ , onCreate onStart ๋“ฑ๊ณผ ๊ฐ™์€ ๋งค์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ ๋ผ์ดํ”„์‚ฌ์ดํด ์ƒํƒœ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ์Œ.
Lifecycle ํด๋ž˜์Šค์˜ addObserver() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์˜ต์ €๋ฒ„ ์ธ์Šคํ„ด์Šค๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์˜ต์ €๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ.




๐Ÿ“– LifecycleOwner

LifecycleOwner๋Š” ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์ผ ๋ฉ”์„œ๋“œ ์ธํ„ฐํŽ˜์ด์Šค. getLifecycle()์ด๋ผ๋Š” ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๋ฉฐ ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„๋˜์–ด์•ผ ํ•จ.
Fragment ๋ฐ AppCompatActivity์™€ ๊ฐ™์€ ๊ฐœ๋ณ„ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋ผ์ดํ”„์‚ฌ์ดํด ์†Œ์œ ๊ถŒ์„ ์ถ”์ƒํ™”ํ•˜๊ณ  ์ด์™€ ์ž‘๋™ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ.

owner๋Š” ๋ผ์ดํ”„์‚ฌ์ดํด์„ ์ œ๊ณตํ•˜๊ณ  observer๋Š” ์ด๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— DefaultLifecycleObserver๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” LifecycleOwner๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์™€ ์›ํ™œํ•˜๊ฒŒ ์ž‘๋™ํ•จ.


โญ๏ธ ์˜ˆ์‹œ - ์œ„์น˜ ์ถ”์  ์˜ˆ์ œ
MyLocationListener ํด๋ž˜์Šค๋ฅผ DefaultLifecycleObserver๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ๋งŒ๋“ค๊ณ  onCreate() ๋ฉ”์„œ๋“œ์—์„œ ์•กํ‹ฐ๋น„ํ‹ฐ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ์ดˆ๊ธฐํ™”ํ•˜๋„๋ก ํ•จ.

๐Ÿ‘‰ ๋ผ์ดํ”„์‚ฌ์ดํด ์ƒํƒœ๋ณ€๊ฒฝ์— ๋Œ€ํ•œ ๋ฐ˜์‘ ๋กœ์ง์ด ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์•„๋‹Œ MyLocationListener์— ์„ ์–ธ๋จ. ๊ฐœ๋ณ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์ฒด ๋กœ์ง์„ ์ €์žฅํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ ๋กœ์ง์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์‰ฌ์›€.

class MyActivity : AppCompatActivity() {
    private lateinit var myLocationListener: MyLocationListener

    override fun onCreate(...) {
        myLocationListener = MyLocationListener(this, lifecycle) { location ->
            // update UI
        }
        Util.checkUserStatus { result ->
            if (result) {
                myLocationListener.enable()
            }
        }
    }
}



์ผ๋ฐ˜์ ์œผ๋กœ ํ˜„์žฌ ๋ผ์ดํ”„์‚ฌ์ดํด ์ƒํƒœ๊ฐ€ ์ข‹์ง€ ์•Š์€ ๊ฒฝ์šฐ ํŠน์ • ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋„๋ก ํ•จ.
-> ์˜ˆ: ์•กํ‹ฐ๋น„ํ‹ฐ ์ƒํƒœ๊ฐ€ ์ €์žฅ๋œ ํ›„(onSaveInstanceState) ํ”„๋ž˜๊ทธ๋จผํŠธ ํŠธ๋žœ์žญ์…˜์„ ์‹คํ–‰ํ•˜๋Š” ์ฝœ๋ฐฑ์ด ์‹คํ–‰๋œ๋‹ค๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์Œ.

์œ„์˜ ์ผ€์ด์Šค๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Lifecycle ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉ.


internal class MyLocationListener(
        private val context: Context,
        private val lifecycle: Lifecycle,
        private val callback: (Location) -> Unit
): DefaultLifecycleObserver {

    private var enabled = false

    override fun onStart(owner: LifecycleOwner) {
        if (enabled) {
            // connect
        }
    }

    fun enable() {
        enabled = true
        if (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
            // connect if not connected
        }
    }

    override fun onStop(owner: LifecycleOwner) {
        // disconnect if connected
    }
}

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•ˆ๋“œ๋กœ์ด๋“œ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•ด์•ผ ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋ฉด lifecylce-aware-components ์‚ฌ์šฉ์„ ๊ถŒ์žฅ.




๐Ÿ“– ์ปค์Šคํ…€ LifecycleOwner ๊ตฌํ˜„

LifecylceOwner๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ์ปค์Šคํ…€ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด LifecycleRegistry ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ฒคํŠธ๋ฅผ ํ•ด๋‹น ํด๋ž˜์Šค๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•จ.

class MyActivity : Activity(), LifecycleOwner {

    private lateinit var lifecycleRegistry: LifecycleRegistry

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

        lifecycleRegistry = LifecycleRegistry(this)
        lifecycleRegistry.markState(Lifecycle.State.CREATED)
    }

    public override fun onStart() {
        super.onStart()
        lifecycleRegistry.markState(Lifecycle.State.STARTED)
    }

    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry
    }
}
  • LifecycleOwner ์‚ฌ์šฉ์ž ์ •์˜ ์ปค์Šคํ…€ ํด๋ž˜์Šค
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry

class CustomLifecycleComponent : LifecycleOwner {
    private val lifecycleRegistry = LifecycleRegistry(this)

    init {
        // Initialize the lifecycle to CREATED state
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
    }

    fun start() {
        // Simulate starting the component
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
    }

    fun stop() {
        // Simulate stopping the component
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
    }

    fun destroy() {
        // Simulate destroying the component
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    }

    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry
    }
}




๐Ÿ“– lifecycle-aware components ๊ถŒ์žฅ์‚ฌํ•ญ

  • UI ์ปจํŠธ๋กค๋Ÿฌ(์•กํ‹ฐ๋น„ํ‹ฐ, ํ”„๋ž˜๊ทธ๋จผํŠธ)๋ฅผ ์ตœ๋Œ€ํ•œ ๊ฐ€๋ณ๊ฒŒ ์œ ์ง€. ๋ฐ์ดํ„ฐ๋ฅผ ํš๋“ํ•˜๋ ค๊ณ  ํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋ฉฐ, ๋Œ€์‹  ๋ฐ์ดํ„ฐ๋ฅผ ํš๋“ํ•˜๋Š”๋ฐ ViewModel์„ ์‚ฌ์šฉํ•˜๊ณ  LiveData ๊ฐ์ฒด๋ฅผ ๊ด€์ฐฐํ•˜์—ฌ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ทฐ์— ๋ฐ˜์˜ํ•ด์•ผ ํ•จ.

  • ๋ฐ์ดํ„ฐ ์ฃผ๋„ํ˜• UI๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•  ๊ฒƒ. UI ์ปจํŠธ๋กค๋Ÿฌ์˜ ์—ญํ• ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ๋ทฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž ์ž‘์—…์„ ViewModel๋กœ ์•Œ๋ฆฌ๋Š” ๊ฒƒ.

  • ๋ฐ์ดํ„ฐ ๋กœ์ง์€ ViewModel ํด๋ž˜์Šค์— ๋‘˜ ๊ฒƒ. ViewModel์€ UI ์ปจํŠธ๋กค๋Ÿฌ์™€ ์•ฑ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์—ญํ• ์„ ํ•ด์•ผ ํ•จ. ํ•˜์ง€๋งŒ ViewModel์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ(์˜ˆ๋ฅผ ๋“ค์–ด ๋„คํŠธ์›Œํฌ๋กœ๋ถ€ํ„ฐ)์— ๋Œ€ํ•œ ์ฑ…์ž„์€ ์—†์Œ. ViewModel์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ์ ์ ˆํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ˜ธ์ถœํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ UI ์ปจํŠธ๋กค๋Ÿฌ์— ์ œ๊ณต.

  • ๋ทฐ์™€ UI ์ปจํŠธ๋กค๋Ÿฌ๊ฐ„์˜ ๊น”๋”ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์œ ์ง€๋ฅผ ์œ„ํ•ด Data Binding์„ ์‚ฌ์šฉํ•  ๊ฒƒ. ๋ทฐ๋ฅผ ๋ณด๋‹ค ์„ ์–ธ์ ์œผ๋กœ ๋งŒ๋“ค๊ณ  ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ ์ž‘์„ฑํ•ด์•ผํ•˜๋Š” ์—…๋ฐ์ดํŠธ ์ฝ”๋“œ๋ฅผ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์Œ.
    Java๋กœ๋Š” Butter Knife ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ.

  • ๋งŒ์•ฝ UI๊ฐ€ ๋ณต์žกํ•˜๋‹ค๋ฉด UI ์ˆ˜์ •์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋ ˆ์  ํ„ฐ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ. UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์‰ฌ์›Œ์ง.

  • ViewModel์—์„œ View ๋˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ ์ปจํ…์ŠคํŠธ๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๋„๋ก ํ•ด์•ผํ•จ. ViewModel์ด ์•กํ‹ฐ๋น„ํ‹ฐ ์ˆ˜๋ช…๋ณด๋‹ค ๊ธธ๋ฉด(์˜ˆ๋ฅผ ๋“ค์–ด ํ™”๋ฉด ํšŒ์ „ ๊ฐ™์€ ๊ตฌ์„ฑ ๋ณ€๊ฒฝ.) ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ์˜ํ•ด ์ œ๋Œ€๋กœ ํ๊ธฐ๋˜์ง€ ์•Š์•„ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ.

  • ๊ธด ์‹คํ–‰ ์ž‘์—… ๋ฐ ๋‹ค๋ฅธ ๋น„๋™๊ธฐ ์ž‘์—… ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•  ๊ฒƒ.




๐Ÿ“– lifecycle-aware components ์‚ฌ์šฉ ์‚ฌ๋ก€

  • ๋Œ€๋žต์ ์ธ(coarse-grained) ์œ„์น˜์™€ ์„ธ๋ถ„ํ™” ๋œ(fined-grained) ์œ„์น˜ ์—…๋ฐ์ดํŠธ ์‚ฌ์ด์˜ ์ „ํ™˜ ์‹œ์— ์‚ฌ์šฉ.
    ๋ผ์ดํ”„ ์‚ฌ์ดํด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜ ์•ฑ์ด ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๋™์•ˆ ์„ธ๋ถ„ํ™”๋œ ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ณ , ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ด๋™ํ•  ๋•Œ ๋Œ€๋žต์ ์ธ ์œ„์น˜ ์—…๋ฐ์ดํŠธ๋กœ ์ „ํ™˜.
    LiveData, lifecycle-aware ์ปดํฌ๋„ŒํŠธ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ์•ฑ์ด UI๋ฅผ ์ž๋™์œผ๋กœ ์—…๋Žƒํ•˜๋„๋ก ํ•จ.

  • ๋น„๋””์˜ค ๋ฒ„ํผ๋ง ์ค‘์ง€ ๋ฐ ์‹œ์ž‘ ์‹œ์— ์‚ฌ์šฉ. lifecycle-aware ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒ„ํผ๋ง์„ ๊ฐ€๋Šฅํ•œ ๋นจ๋ฆฌ ์‹œ์ž‘ํ•˜๊ณ  ์•ฑ์ด ์™„์ „ํžˆ ์‹œ์ž‘ํ•  ๋•Œ๊นŒ์ง€ ์žฌ์ƒ ์—ฐ๊ธฐ. ์•ฑ์ด ์ข…๋ฃŒ๋  ๋•Œ ๋ฒ„ํผ๋ง์„ ๋นจ๋ฆฌ ์ค‘๋‹จํ•˜๋Š”๋ฐ์—๋„ ์‚ฌ์šฉ๋จ.

  • ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์˜ ์‹œ์ž‘ ๋ฐ ์ค‘์ง€์— ์‚ฌ์šฉ. lifecycle-aware ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ์ด ํฌ๊ทธ๋ผ์šด๋“œ์— ์žˆ์„ ๋•Œ ๋„คํŠธ์›Œํฌ ๋ฐ์ดํ„ฐ ๋ผ์ด๋ธŒ ์—…๋ฐ์ดํŠธ(์ŠคํŠธ๋ฆฌ๋ฐ)์„ ํ™œ์„ฑํ™”, ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ „ํ™˜๋  ๋•Œ ์ž๋™์œผ๋กœ ์ผ์‹œ์ค‘์ง€๋˜๋„๋ก ํ•จ.

  • ์• ๋‹ˆ๋ฉ”์ด์…˜ drawable์˜ ์ผ์‹œ ์ค‘์ง€ ๋ฐ ์žฌ๊ฐœ. lifecycle-aware ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์žˆ์„ ๋•Œ ์• ๋‹ˆ๋ฉ”์ด์…˜ drawable์„ ์ผ์‹œ์ค‘์ง€ํ•˜๊ณ  ์•ฑ์ด ๋‹ค์‹œ ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ์ „ํ™˜๋˜๋ฉด drwable์„ ์žฌ๊ฐœํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•จ.

post-custom-banner

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