3
votes

In iOS 4, if I close & re-open my app rapidly then (after applicationWillResignActive invocation) there is a chance for applicationWillEnterForeground to be get called well before the applicationDidEnterBackground and results in a black blank screen as because the app entered into background state immediately after the foreground state.

this is the order it was printed in the console:

* 1. applicationWillResignActive
2. applicationDidEnterBackground
3. applicationWillEnterForeground
4. applicationDidBecomeActive
1. applicationWillResignActive
3. applicationWillEnterForeground
2. applicationDidEnterBackground *

How to handle such scenario? and to make sure that application delegate methods are executed in the correct order?

Thanks in advance.

2
Step 1: FILE A BUG! It sounds like this is a real problem; it should get fixed! - jtbandes

2 Answers

1
votes

Keep a counter for switches and ignore switches that happen in the wrong order. Something like this:

-(void) handleSwitchToBackground {
  if ( myState == 0 ) { /* do background stuff */ }
  myState += 1;
}

-(void) handleSwitchToForeground {
  myState -= 1;
  if ( myState == 0 ) { /* do foreground stuff */ }
}

If foreground happens before background, neither method does anything.

0
votes

This happens to me as well.

Only in my case it sometimes happens when my app receives local notification.