My Android application does some calculations, but I noticed recently that if I were to navigate away from the application, the thread that was running and doing the work stops.
This also happens if the phone goes in standby mode, or the user locks it.
I want to make my application continue to do it’s work even if the user navigates away from it, or locks phone(or if the phone goes into standby mode).
I am using all of the Activity lifecycle methods, but I do not destroy anything unless the user actually presses ‘Back’ on his phone’s physical button OR presses ‘Exit’ from the app’s menu.
The thread which does the calculations lies in it’s own public method, however it is invoked from a button handler’s onclick method. This handler, and pretty much all of them are defined in the onCreate method.
Your
Activitywill be paused when the device’s screen turns off (and resumed when the screen turns on).Consider doing your work in a
Serviceinstead.IntentServiceis one relatively easy way to do this because it takes care of creating a separate thread, processing your work requests one at a time, and cleaning up when there is no more work to be done.