0
0
Android Kotlinmobile~10 mins

Saving instance state in Android Kotlin - UI Render Trace

Choose your learning style9 modes available
Component - Saving instance state

This component shows how an Android app saves and restores its state when the screen rotates or the app is temporarily stopped. It keeps the user input safe so it doesn't disappear.

Widget Tree
Activity
├── LinearLayout (vertical)
│   ├── EditText
│   └── Button
└── TextView
The main screen is an Activity with a vertical LinearLayout. Inside it, there is an EditText for user input, a Button to save the input, and a TextView to display the saved text.
Render Trace - 5 Steps
Step 1: Activity
Step 2: User Interaction
Step 3: Button
Step 4: Activity
Step 5: Activity
State Change - Re-render
Trigger:Device rotation causing Activity recreation
Before
EditText contains user typed text, TextView shows saved text
After
EditText and TextView restored with the same text after rotation
Re-renders:Entire Activity UI re-created, but text content restored from saved state
UI Quiz - 3 Questions
Test your understanding
What happens when the device rotates in this app?
AThe Activity is destroyed and recreated, but user text is restored
BThe Activity stays the same and nothing changes
CThe user text is lost and EditText is empty
DThe app crashes because state is not saved
Key Insight
Saving instance state in Android helps keep user data safe during screen rotations or temporary stops. Using onSaveInstanceState and restoring in onCreate ensures a smooth user experience without losing input.