0

is it possible to intercept the event of system closing my application on low memory? I know there is a method "onLowMemory" but it doesn't mean that my application will be closed after it is invoked.

My problem is that I'm co-developing an app that uses a lot of static data (which I know should be avoided, but that's not my decision and at this point it's almost impossible to change this fact). If I press home and don't get back to this application for eg. 10 hours it would probably force close when I do because the static data isn't there anymore (app got killed by the system).

Is it possible to know when the app is about to get killed by the system? I would store the static data somewhere to get it later then, when I recreate my app. What are possible solutions?

Maybe, if hooking to such an event is impossible, I could make a service that saves this state every few minutes, so when app gets killed I would probably have the latest static data?

Did you encounter this problem? What are your solutions/suggestions?

trincot
  • 317,000
  • 35
  • 244
  • 286
kajman
  • 1,066
  • 11
  • 24
  • If the data isn't there anymore after 10hours, it's probably because your app was killed. In which case you will have to load the data. If it force closes, wouldn't it always force close because when the app is started for the first time, the data is not there. onLowMemory() is a best attempt. There is also `onTrimMemory()` which gives you levels of memory left. I'm sorry this is a very vague question and I think there might be a misunderstanding of how processes, memory and state work/are related to your app, within the OS – Greg Giacovelli Oct 22 '12 at 09:41
  • @GregGiacovelli I know it is because my app is killed. Force closes are related to app not being able to load this static data (all references are null etc). I was hoping just to find the best timing to save the data (just before it gets out of memory - geting killed by OS). – kajman Oct 22 '12 at 13:06

1 Answers1

1

Does your app do anything with this static data in the background? You could just save everything in onPause(). Once onPause has been called your app is killable at any time without any further callbacks, so if something needs to be saved, onPause is the place to do it.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • ... or something similar. Even if you can't change the architecture, it might be a good idea to change something about it to ensure that your data is saved reliably. Saving the data when changed, some transactional or idempotent changes, etc. could be better alternatives. Otherwise, you will still have the probability of data loss. – full.stack.ex Oct 25 '12 at 21:38