SuperGodSachin
SuperGodSachin

Reputation: 63

In iOS4 applicationWillEnterForeground may get called before applicationDidEnterBackground and it makes problem

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.

Upvotes: 3

Views: 2355

Answers (2)

drawnonward
drawnonward

Reputation: 53669

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.

Upvotes: 1

Kof
Kof

Reputation: 1

This happens to me as well.

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

Upvotes: 0

Related Questions