Instead of relying on the applicationState value, how about posting a custom notification at the end of the background task?
Set up as an observer for your custom notification:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundTaskDidFinish:) name:@"BackgroundTaskDidFinish" object:<either nil or the object that will post the notification>];
}
Post your custom notification when the background task completes
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
backgroundTaskID = UIBackgroundTaskInvalid;
[[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundTaskDidFinish" object:self]
}
Respond to the notification, which your app should receive when it really returns to an active state
- (void)backgroundTaskDidFinish:(NSNotification *)notification
{
//OpenGL operations
}
Alternatively, you could look for one of the other notification messages that would indicate that your app is truly in an active state.
For instance, add your object as an observer for UIApplicationWillEnterForeground. Set a BOOL value when you receive that notification. Then, look for that BOOL value along with the UIApplicationState value.
Of course, at some point you'll have to clear the BOOL - say, when you're starting that background task - so it will be NO unless the app really did become active.