my app, how do I preserve the state of the timer? If the device is rotated, the timer start afresh, I want it to continue.
Option 1: In onSaveInstanceState, save the current value of the timer (probably a long, but depends) in the bundle. In onRestoreInstanceState, fetch that long, set that as your timer's initial value, and set the timer running. Option 2: Put the timer in a ViewModel, and use LiveData to talk back to the activity. That will take care of everything. Depending on your current architecture, option 1 might suit you better.
Обсуждают сегодня