<Android> Configuration change๋ž€?

์ง„์„ญยท2025๋…„ 1์›” 10์ผ
0
post-thumbnail

๐Ÿ“ŒConfiguration change๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

ํ•œ๊ตญ ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” ๊ตฌ์„ฑ ๋ณ€๊ฒฝ ์ฒ˜๋ฆฌ๋ผ๋Š” ์ œ๋ชฉ์œผ๋กœ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ ๋ฌธ์„œ์—์„œ๋Š” ์‹œ์Šคํ…œ์€ ๊ตฌ์„ฑ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜๋ฉด Activity๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ ๋‹ค๊ณ  ๋‚˜์™€ ์žˆ๋Š”๋ฐ ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํ™”๋ฉด ๋ฐฉํ–ฅ์ด ๋ณ€๊ฒฝ์ด ๋˜๋ฉด ์ด๊ฒŒ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜๋Š” ์ด๋ฒคํŠธ๋‹ˆ Activity๊ฐ€ ์†Œ๋ฉธํ•˜๊ณ  ๋‹ค์‹œ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“ŒConfiguration change์‹œ ํ˜ธ์ถœ๋˜๋Š” ์ƒ๋ช…์ฃผ๊ธฐ๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€?

๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ์‹œ์Šคํ…œ์€ ๊ตฌ์„ฑ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜๋ฉด Activity๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์‹œ์Šคํ…œ onDestroy()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ธฐ์กด Activity ์ธ์Šคํ„ด์Šค๋ฅผ ์†Œ๋ฉธ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ onCreate()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ์ด ์ƒˆ Activity ์ธ์Šคํ„ด์Šค๋Š” ์—…๋ฐ์ดํŠธ๋œ ์ƒˆ ๊ตฌ์„ฑ์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์‹œ์Šคํ…œ์—์„œ๋„ ์ƒˆ ๊ตฌ์„ฑ์œผ๋กœ UI๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

์‰ฝ๊ฒŒ ์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Activity ์†Œ๋ฉธ

  • ๊ตฌ์„ฑ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜๋ฉด ๊ธฐ์กด Activity๋Š” onDestroy()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉฐ ์†Œ๋ฉธ ๋ฉ๋‹ˆ๋‹ค.

Activity ์žฌ์ƒ์„ฑ

  • ๊ทธ ๋‹ค์Œ onCreate()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉฐ ์ƒˆ๋กœ์šด Activity ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ์ œ๊ณตํ•˜๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๊ทธ๋ฆผ์ธ๋ฐ ์ด ๊ทธ๋ฆผ์„ ๋ณด๋ฉด์„œ ์ดํ•ด๋ฅผ ํ•˜๋ฉด ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ์‹ค์Šตํ•ด๋ณด๊ธฐ

์‚ฌ์‹ค ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” Compose์— ๋Œ€ํ•ด์„œ๋„ ๋‚˜์™€ ์žˆ์ง€๋งŒ ์ €๋Š” ์•„์ง Compose์— ์ต์ˆ™์ง€ ์•Š์•„ XML๋กœ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ œ๊ฐ€ ๋งŒ๋“  ์•ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • TextView, Button 2๊ฐœ์˜ ์œ„์ ฏ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • Button์„ ํด๋ฆญํ•˜๋ฉด TextView Conter๊ฐ€ 1 ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • ํ™”๋ฉด๋„ ๋Œ๋ ค๋ณด๊ณ  ๋‹คํฌ๋ชจ๋“œ๋„ ํ•ด๋ณด๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • Configuration change์‹œ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด Logcat์„ ์ฐ์–ด๋ด…๋‹ˆ๋‹ค.

<XML์ฝ”๋“œ>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <TextView
        android:id="@+id/counterText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/TextViewConter"
        android:textSize="18sp"
        android:layout_marginTop="16dp" />

    <Button
        android:id="@+id/incrementButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ํด๋ฆญ"
        android:layout_marginTop="16dp" />
</LinearLayout>

<MainActivity ์ฝ”๋“œ>

class MainActivity : AppCompatActivity() {

    // Log ํƒœ๊ทธ
    private val TAG = "MainActivityLifecycle"
    private lateinit var binding: ActivityMainBinding
    private var counter = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        Log.d(TAG, "onCreate")
        binding.incrementButton.setOnClickListener {
            counter++
            binding.counterText.text = "Counter: $counter"
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart")
    }
}

๊ฒฐ๊ณผ

๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ˆซ์ž๋ฅผ ์ฆ๊ฐ€ ์‹œํ‚จ ํ›„ ํ™”๋ฉด ํšŒ์ „๊ณผ ๋‹คํฌ๋ชจ๋“œ๋กœ ๋ณ€๊ฒฝ์„ ์ง„ํ–‰ํ•œ ๊ฒฐ๊ณผ ์ฆ๊ฐ€๋œ ์ˆซ์ž๋Š” ๋‹ค์‹œ 0์œผ๋กœ ๋ณ€ํ•˜๋Š” ๊ฑธ ํ™•์ธ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Logcat๋„ ํ™•์ธํ•ด ๋ณด๋ฉด onPause onStop onDestroy ๊ฐ€ ์ง„ํ–‰์ด ๋˜๊ณ  onCreate onStart onResume ์ˆœ์œผ๋กœ ๋‹ค์‹œ ๋ทฐ๊ฐ€ ์ƒ์„ฑ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์ด์ œ Configuration change๊ฐ€ ์ผ์–ด๋‚˜๋ฉด data๊ฐ€ ์—†์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ data๋ฅผ ์œ ์ง€ํ•ด์•ผํ•˜๋Š”๋ฐ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋กœ์ปฌ DB(SharedPreference, DataStore, Room),viewModel,SavedInstanceState (SavedStateHandle)๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…์€ Configuration change๊ฐ€ ์ผ์–ด๋‚˜๋ฉด data๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•˜์—ฌ ์ž‘์„ฑํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ์ฐธ๊ณ ์ž๋ฃŒ

ํ™œ๋™ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๊ณต์‹ ๋ฌธ์„œ
๊ตฌ์„ฑ ๋ณ€๊ฒฝ ์ฒ˜๋ฆฌ ๊ณต์‹ ๋ฌธ์„œ

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

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด