开发者

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

开发者 https://www.devze.com 2023-01-08 01:23 出处:网络
In iOS 4, if I close & re-open my app rapidly then (after applicationWillResignActive invocation) there is a chance forapplicationWillEnterForegroundto be get called well before theapplicationDidE

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 exe开发者_运维百科cuted in the correct order?

Thanks in advance.


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.


This happens to me as well.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消