0
0
Android Kotlinmobile~5 mins

Why ViewModel survives configuration changes in Android Kotlin

Choose your learning style9 modes available
Introduction

A ViewModel keeps your app data safe when the screen changes, like rotating your phone. It helps avoid losing information and keeps the app smooth.

When you want to keep user data safe during screen rotation.
When your app fetches data from the internet and you don't want to reload it after a configuration change.
When you want to keep UI state like scroll position or form inputs during device rotation.
When you want to separate UI code from data handling for cleaner code.
When you want to avoid restarting long tasks like downloads or calculations on screen changes.
Syntax
Android Kotlin
class MyViewModel : ViewModel() {
    // Your data and logic here
}

// In your Activity or Fragment
val viewModel = ViewModelProvider(this).get(MyViewModel::class.java)

The ViewModel class is part of Android Jetpack libraries.

Use ViewModelProvider to get the ViewModel instance tied to the UI controller lifecycle.

Examples
A simple ViewModel holding a count number.
Android Kotlin
class CounterViewModel : ViewModel() {
    var count = 0
}
Accessing and updating the count in the ViewModel from an Activity or Fragment.
Android Kotlin
val viewModel = ViewModelProvider(this).get(CounterViewModel::class.java)
viewModel.count += 1
Sample App

This app shows a number and a button. When you tap the button, the number goes up. If you rotate the phone, the number stays the same because the ViewModel keeps the count safe.

Android Kotlin
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class CounterViewModel : ViewModel() {
    var count = 0
}

class MainActivity : AppCompatActivity() {
    private lateinit var viewModel: CounterViewModel

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

        viewModel = ViewModelProvider(this).get(CounterViewModel::class.java)

        val textView = TextView(this).apply {
            text = "Count: ${viewModel.count}"
            textSize = 24f
            id = 1
        }

        val button = Button(this).apply {
            text = "Increase Count"
            setOnClickListener {
                viewModel.count += 1
                textView.text = "Count: ${viewModel.count}"
            }
        }

        val layout = android.widget.LinearLayout(this).apply {
            orientation = android.widget.LinearLayout.VERTICAL
            addView(textView)
            addView(button)
        }

        setContentView(layout)
    }
}
OutputSuccess
Important Notes

ViewModel objects survive configuration changes like screen rotation but are cleared when the Activity or Fragment is finished.

Do not store UI elements or Context in ViewModel to avoid memory leaks.

ViewModel helps keep your app fast and user-friendly by saving data across changes.

Summary

ViewModel keeps data safe during screen rotations and other configuration changes.

It separates UI code from data logic for cleaner apps.

Use ViewModelProvider to get your ViewModel instance tied to your UI lifecycle.