Sometimes my app is forced to quit and then later Android restarts it just to run a scheduled job. I can't find a callback that would correspond to this scenario: that my app got killed in the meantime, so I have to restore all my retained state.
I want to avoid a proliferation of entry points where I have to re-check whether everything is still in place. I already have onUpdate()
for a widget, onCreate()
for the main activity, onResume()
for a view fragment, and onStartJob()
for the scheduled job. Any of them could be the first thing to be called after the app is killed, so in all these places I have to repeat the initialization code.
Is there a single point where I can register a callback that will re-initialize my app state?
To be specific, I have a JobService:
class RefreshImageService : JobService() {
override fun onStartJob(params: JobParameters): Boolean {
MyLog.i("RefreshImageService start job")
updateWidgetAndScheduleNext(applicationContext)
return true
}
override fun onStopJob(params: JobParameters): Boolean {
MyLog.i("RefreshImageService stop job")
return true
}
}
The updateWidgetAndScheduleNext()
call involves some state I have:
private data class TimestampedBitmap(val bitmap : Bitmap, val timestamp : Long)
private var tsBitmap: TimestampedBitmap? = null
To compute the timestamp, I have to call into further code that has its own state; that other code is called form all the entry points I mentioned. I'd like to centralize my initialization code, if possible.