In an iOS4 application, WillEnterForeground can be called before applicationDidEnterBackground, and this creates a problem

In iOS 4, if I quickly close and reopen the application (after calling applicationWillResignActive ), there is a chance that applicationWillEnterForeground will be called long before applicationDidEnterBackground and displays a black blank screen because the application enters the background state immediately after the foreground state.

this is the order he printed in the console:

<i> * 1. applicationWillResignActive
2. applicationDidEnterBackground
3. applicationWillEnterForeground
4. applicationDidBecomeActive
1. applicationWillResignActive
3. applicationWillEnterForeground
2. applicationDidEnterBackground *

How to cope with such a scenario? and make sure that the delegation methods of the application are executed in the correct order?

Thanks in advance.

+3
source share
2 answers

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

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

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

If the foreground occurs in front of the background, none of them does anything.

+1

.

, .

0

Source: https://habr.com/ru/post/1755778/


All Articles