0

I have an android Activity extending AppWidgetProvider in which I'd like to keep a count of how many times onReceive() is called. The problem is, every once in a while there is an entry in Logcat saying "No longer want blah.foo.bar.baz" and shortly afterwards an entry saying "Scheduling restart of crashed service blah.foo.bar.baz". Android kills and then restarts my process which causes all my variables to initialize to default values. Only way I can think of to maintain variable state is to write them out to disk using SharedPreferences when Android "No longer wants.." and then reading them back in when "Scheduling restart..". Any other ways to do this?

wufoo
  • 13,571
  • 12
  • 53
  • 78

1 Answers1

1

The problem is, every once in a while there is an entry in Logcat saying "No longer want blah.foo.bar.baz" and shortly afterwards an entry saying "Scheduling restart of crashed service blah.foo.bar.baz". Android kills and then restarts my process which causes all my variables to initialize to default values.

This is perfectly normal.

Only way I can think of to maintain variable state is to write them out to disk using SharedPreferences when Android "No longer wants.." and then reading them back in when "Scheduling restart..".

That will not work, as you do not get control at those points in time.

Any other ways to do this?

Persist your data when the data changes (database, SharedPreferences, or file). Read in the data when you need it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491