Search code examples
androidwakelock

Does Android have a means of storing an app's state to disk


I would like to perform a wakelock at certain time intervals in my app. It would be nice if Android could just store the entire state of my app to disk prior to the wakelock being released and then reloaded when an AlarmManager alarm or some other process wants to startup my app. Currently I store a lot of variables to disk and reload them when the app is restarted again. Microsoft Windows does this if you go into stand-by mode. Just wondering whether Android supports a similiar kind of thing.


Solution

  • I don't think there is good way to store whole application state. There is no need to store every single variable when saving state of you application. Most variables would be propably pointers to runtime objects like surfaces. And such should not be saved, as well as any decompressed images, sounds etc. Instead, on resume, you should load them from APK resources again. I don't think there is efficient way how to do this without programmers work. Sorry, someone has to know what application loads.

    I think you misunderstood way smartphone works and behaves. Or maybe I misunderstand your question. If you were talking about MS Windows on desktop and how they hibernate, that is completely different from how phones work and how users will use them. On Windows CE, sleep to memory may work well, you just store everything you did and stop CPU. On single purpose device, like navigation device, it is quite sufficient and well working. You turn off navigation by turning off device, and power on means you start navigation again. Resume of complete OS is fine here.

    But smart phones will not go to sleep as whole device. They have many applications. And if one application does not need to do anything, that does not mean whole device can sleep because of that. It is phone, you have to be able to accept calls. Receive mails. Use GPS to record your track. OS itself MUST continue to run, unlike hibernated windows.

    So just serialize minimum of information you need to restore to your old state. System cannot do it for you, as network connections you created a hour ago are invalid, files could be moved by other application. If your activity or service is started by AlarmManager, you have to store what you did before yourself. Use SharedPreferences or Sqlite database. If you have simple data, use OnSaveInstanceState bundle only.

    Do you want to explain what are you trying to accomplish? I think you want to design your application in a wrong way.