When i am put my application 开发者_JAVA技巧to the background and again awake it means all the Global variables are cleared.This is not happening always but at rare case only.I guess that could be memory crash only.Why global values are cleared? how to recover from this?
This might be the way Android works. Just because you close your last activity in your application doesn't mean that your application is cleared from memory. If Android doesn't need the memory space taken up by your application it won't necessarily be killed right away by Android. Also the garbage collector has it's own life cycle which doesn't necessarily get triggered on each activity destroy.
You can try this: Start Angry Birds and start playing around, fire off some "crazy chickins". Press the home button in the middle of an ongoing game and start Angry Birds directly again. Notice how you end up on the very same level you just "home-ed" out from. Press the home button again and start another application (the browser is usually memory hungry). After some time start Angry Birds again and notice how you'll have to go through the entire start up sequence again (i.e. you don't necessarily end up in the middle of the game, on the level you previously left).
If you really, really need to maintain application state, regardless of GC run or no-run, you should probably use SharedPreferences or a SQLite database. You can read up more on these concepts here: http://developer.android.com/guide/topics/data/data-storage.html
精彩评论