1

In my app, I have a loading view that sets up the app by going out to an API and bringing back some startup info to store. I store it temporarily in an Application class. An example of the type of data is lets say a list of servicable cities.

The flow is:

Loading View -> Main Menu -> etc etc

If I exit out of the app and come back, it usually starts with Main Menu view and hits onresume and everything is fine. But sometimes if I wait a while and the OS clears some memory maybe, I come back and it again goes to Main Menu but this time starts from scratch for that view by calling onCreate. But it seems like the application data is wiped.

I am just wondering why, if it is wiping the application data, is it not just starting up the app from scratch?

Is there some directive in the manifest I can make to make it do this? Keep in mind, I dont want it to restart the app when coming back into the app from a pause. Only when the OS has cleared the application data...

Jesse
  • 2,674
  • 6
  • 30
  • 47

1 Answers1

2

When the application is closed to free up memory the state of stored in your Application class is lost. You can save information that will persists even if you application gets closed in a few ways.

One is to use shared preferences: Making data persistent in android

The other is to use Bundles which is probably more along the lines of what you need: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
slayton
  • 20,123
  • 10
  • 60
  • 89
  • @Jesse This is right. And please review [activity lifecycle](http://developer.android.com/reference/android/app/Activity.html). –  May 09 '12 at 17:00
  • Well if the application is being closed, why doesnt it start back up with the Loading View? If that would happen, everything would be fine. – Jesse May 09 '12 at 18:35
  • I am already using SharedPreferences for some things, but dont see why I need to here...these are simple things such as flags and a couple light weight collections. – Jesse May 09 '12 at 18:37
  • `SharedPreferences` are **perfect** for storing flags! Collections probably belong better in a `Bundle`. The problem is that when your app closes it could be in `Paused`, `Stopped`, or `Destroyed` in the case that it is `Destroyed` nothing is left. All objects are deleted and all memory is freed. If you haven't save state using `SharedPreferences`, `Bundles`, or using a file on the disk then everything will be lost. – slayton May 09 '12 at 18:59