Search code examples
androidonpause

Does onPause get called when the device suddenly turns off


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


Solution

  • 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.