Why Early Android Apps Feel Fragile
In fast tutorial-style Android projects, screen logic often gets embedded directly in Activities. It works during demos but breaks under lifecycle and rotation events.
Step 1: Move state out of Activity fields
data class QuizState(
val index: Int = 0,
val score: Int = 0,
val finished: Boolean = false
)
Step 2: Handle events through a reducer
fun reduce(state: QuizState, correct: Boolean): QuizState {
val nextScore = if (correct) state.score + 1 else state.score
val nextIndex = state.index + 1
return state.copy(index = nextIndex, score = nextScore, finished = nextIndex >= 10)
}
Step 3: Render UI from immutable snapshot
Every render cycle should read from one state object, not multiple mutable globals.
Pitfalls
- State reset on rotation because values live only in Activity fields.
- UI widgets used as the source of truth.
- No event tests for end-of-quiz transitions.
Verification
- Progress survives lifecycle recreation events.
- Reducer tests cover all event branches.
- Final score remains stable across orientation changes.