I have a doubt. Suppose we have an android application running and the devices goes for Auto Power OFF eg.- if the battery is pulled out. In this case the device will suddenly switch off and the application will be killed. So does onPause/ onStop get called in this scenario ? In other words how do we save data in our application when such situation happens ? Thanks, Ankit
Asked
Active
Viewed 303 times
0
-
3If you want to cover that situation you need to save data periodically. Each ex. 2 mins. If someone pulls of battery every application state is like nuked. There's nothing left of it :) – meeDamian Aug 09 '12 at 11:52
-
@Ankit - you are seriously asking whether onPause is called when the battery is pulled out? Whatever minuscule power the device has remaining in such a situation is much better used in making sure memory is in a stable state so that the device isn't completely trashed - not in executing whatever code some app developer thinks is important. If you'd asked about some "low battery" trigger, or when the user hits "power" button, the question might be sensible... – ToolmakerSteve Sep 26 '15 at 17:06
-
@ToolmakerSteve - When I asked I was serious, because I working on a customized android platform for a non mobile device. So it was easier to out the battery and the SQA had logged several defects relating to this case. Obviously no one in my team back then had clear ideas about it. I completely understand ur argument and now the question seems silly to me as well. But this was like 3.5 yrs ago and the situation confused me back then, so I sought the views of my community. – Ankit Sep 28 '15 at 05:28
1 Answers
0
The answer is no, no method is called in case of battery removal.
Think about it... if you pull the battery, there is no power to run the device, so nothing "gets called". It all just goes away.
There are basically two options to protect your data.
1) Save at some (reasonable) interval
2) Save immediately upon receiving data
I usually go with option 2, although I can see there might be reasos to go with option 1.

Barak
- 16,318
- 9
- 52
- 84