Android Development

    Android ViewModel vs onSaveInstanceState vs SavedStateHandle – Process Death Explained

    A
    AzeemJan 25, 2026 · 5 min read
    144
    Android ViewModel vs onSaveInstanceState vs SavedStateHandle – Process Death Explained

    Android State Management Explained: ViewModel, onSaveInstanceState, and SavedStateHandle

    Android interviews and real-world apps often fail for one simple reason: state is misunderstood.

    Beginners struggle because screens reset unexpectedly. Senior engineers struggle because apps break under configuration changes or process death.

    This article starts from absolute basics and gradually builds up to senior-level mental models so you understand what survives, what dies, and why.

    Lets Learn Android state management from basics to advanced. Understand ViewModel, onSaveInstanceState, SavedStateHandle, configuration changes, and process death with real-world examples.

    1. What Is "State" in Android?

    State is any data that represents the current condition of your UI or app.

    Examples:

    • Text typed into a search box

    • Scroll position of a list

    • Selected tab

    • Logged-in user info

    • API response currently shown on screen

    If this data is lost unexpectedly, the user experiences bugs.

    2. Two Types of App Disruptions

    Before learning APIs, you must understand why state is lost.

    2.1 Configuration Changes

    Configuration changes occur when:

    • Screen rotation

    • Dark / Light mode change

    • Language change

    • Font size change

    • Split screen / multi-window

    📌 Important:

    Configuration change does NOT kill the app process, but destroys and recreates the Activity.

    2.2 Process Death

    Process death happens when:

    • App goes to background and system needs memory

    • User enables “Don’t keep activities” (developer option)

    • OS kills the app silently

    📌 Important:

    Process death kills everything in memory.

    This is where many apps fail.

    3. Activity Lifecycle Reality Check

    On rotation or config change:

    Activity → onDestroy()
    Activity → recreated → onCreate()
    
    code

    On process death:

    App process killed → memory cleared
    User returns → app restarted from scratch
    
    code

    So the key question becomes:

    Where should I store my data so it survives the right scenarios?

    4. onSaveInstanceState (The Oldest Tool)

    4.1 What Is onSaveInstanceState?

    onSaveInstanceState() allows you to save small UI-related data into a Bundle.

    It is called:

    • Before Activity is destroyed for configuration change

    • Sometimes before process death

    4.2 What It Is Good For

    Simple UI state

    • Input text

    • Selected item index

    • Scroll position

    Not for:

    • Large objects

    • Network responses

    • Business logic

    4.3 Real-World Example

    Search screen:

    • User types text

    • Rotates phone

    Without saving:

    • Text disappears

    With onSaveInstanceState:

    • Text restored

    4.4 Key Limitations

    • Bundle size limit

    • Serialization overhead

    • Manual and error-prone

    📌 Rule:

    Use it only for temporary UI state, not app logic.

    5. ViewModel (The Game Changer)

    5.1 Why ViewModel Exists

    ViewModel solves one problem:

    Survive configuration changes without reloading data.

    5.2 What ViewModel Actually Is

    • Stored outside Activity lifecycle

    • Lives as long as Activity is alive in memory

    • Destroyed only when Activity is permanently finished

    📌 Important:

    ViewModel survives configuration changes but does NOT survive process death.

    5.3 Real-World Example

    Feed Screen:

    • API call fetches posts

    • Rotate device

    Without ViewModel:

    • API called again ❌

    With ViewModel:

    • Data reused from memory ✅

    5.4 What Should Go in ViewModel

    • UI state, Screen-level business logic, API responses, Loading / error state

    • Thereshould not be Context references and Views

    6. Process Death: The Hidden Interview Trap

    Many developers assume ViewModel is enough.

    It is not.

    When the process dies:

    • ViewModel is destroyed

    • Memory cleared

    • App restarted

    If you don’t handle this:

    • Blank screens

    • Crashes

    • Lost user progress

    7. SavedStateHandle (The Missing Link)

    7.1 What Is SavedStateHandle?

    SavedStateHandle is a key-value storage attached to ViewModel that:

    • Automatically saves data

    • Restores it after process death

    Think of it as:

    onSaveInstanceState for ViewModel

    7.3 Real-World Example

    Search screen:

    • User enters query

    • App goes background

    • OS kills app

    • User returns

    Without SavedStateHandle:

    • Query lost ❌

    With SavedStateHandle:

    • Query restored ✅

    7.4 What to Store in SavedStateHandle

    ✅

    • Search query

    • Selected filters

    • Pagination cursor

    ❌

    • Huge lists

    • Bitmaps

    8. Putting It All Together (Senior Mental Model)

    Where Should Data Live?

    Data Type

    Correct Place

    Input text

    SavedStateHandle

    API response

    ViewModel

    Scroll position

    onSaveInstanceState

    Auth token

    DataStore / DB

    Cached data

    Database

    9. Real App Flow Example (End-to-End)

    Example: Product Listing Screen

    • ViewModel:

      • Fetches products

      • Exposes UI state

    • SavedStateHandle:

      • Current page

      • Selected filter

    • onSaveInstanceState:

      • Scroll position

    Result:

    • Rotation safe

    • Process death safe

    • Smooth UX

    10. Common Interview Questions

    Q: Does ViewModel survive process death?

    ❌ No

    Q: What survives process death?

    • SavedStateHandle

    • Database

    • DataStore

    Q: When to use onSaveInstanceState vs SavedStateHandle?

    • UI-only → onSaveInstanceState

    • ViewModel-related → SavedStateHandle

    11. Final Rules to Remember

    1. Activity is disposable

    2. ViewModel survives rotation, not death

    3. Process death is real

    4. State must have an owner

    5. Persistence beats memory


    12. Why This Matters for Senior Engineers

    Senior Android developers are evaluated on:

    • Correct state ownership

    • Process death handling

    • Predictable behavior

    • Clear mental models

    If you understand this article deeply, you are already ahead of most candidates.

    Want to Learn This for Interviews?

    If you want to move from feature-level coding to system-level thinking, I teach these concepts in 10-Week Job Ready Android Bootcamp focused on real-world architecture, system design, and interview preparation.

    You’ll learn how senior engineers design, reason, and explain Android systems—not just write code.

    Enroll Now for current Batch

    https://thecodingshef.in/course/master-android

    Keep learning. Design with intent.

    android kotlin
    compose
    Was this helpful?

    Comments (0)

    No comments yet. Be the first to share your thoughts!

    Leave a Comment

    Get the latest updates

    Join our newsletter and never miss an article.

    By subscribing, you agree to our Privacy Policy and consent to receive updates.

    Ready to Start Learning?

    Join thousands of students building their dream careers

    Explore Courses