views:

487

answers:

1

I use a single static class in my code that defines a static field which I'm reusing between Activity onStop/onStart invocations. Here's a scenario:

  1. User clicks on "Authorize" button (static data is initialized)
  2. Activity is stopped and web browser is called
  3. Browser executes callback and Activity is restored (static data is reused)

At least one of my users reports the failure at step 3 which I cannot reproduce but which looks like reset of static data

Any suggestions?

+6  A: 

That is not safe. Your process can be killed between onStop and onStart, so all static data will be gone. In fact your activity can even be killed before it gets to onStop. In your tests the process was not killed, but it was for the user. See the Android activity life cycle which has a nice flow chart showing the possibilities.

You need to store the data some other way, in prefs or database for example.

Heikki Toivonen
Thanks Heikki. I think I know what to do at this point
DroidIn.net
Heikki, what do you think about placing the static var in the application context ? Is it safe that way ?
Guido
That should be safe, but I don't see the point in making a static var in an object that lives as long as the process is alive. A normal var should be enough in both the application context and application object itself.
Heikki Toivonen