Reputation: 4843
I'd like to close a modal view controller if my application enters the background. I assumed by putting the code in applicationDidEnterBackground it would do the trick, but unfortunately the command isn't applied to the screen until the next time the app enters the foreground. For a split second you can see the original modal view controller, which then disappears.
Is there any kind of method like applicationWillEnterBackground? Or is there a way of updating the UI before the app goes into the background.
At the moment I have this code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
[splitVO dismissViewControllerAnimated:true completion:nil];
}
Upvotes: 2
Views: 2096
Reputation: 317
Your delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return. In practice, this method should return as quickly as possible. If the method does not return before time runs out, your application is killed and purged from memory. If you still need more time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request background execution time and then start any long-running tasks in a secondary thread. Regardless of whether you start any background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.
Basically, viewDidAppear gets called after your UIViewController's view was added to the application's UIWindow heirarchy. Backgrounding then restoring the app doesn't change your view in that respect, so viewDidAppear doesn't get called -- it's correct behaviour, and not a bug. Check out the API docs for UIViewController.
Upvotes: 0
Reputation: 177
You can try adding code that makes UI change to the following method of application delegate.
-(void)applicationWillResignActive:(UIApplication *)application{
}
This method of application delegate will be called when the application is still on screen but it is going to transition to inactive state and then it will enter Background state in a brief time period viz. applicationDidEnterBackground
method will be executed right after the above method. So here is the right place to do your UI changes. And while bringing the app back from background to foreground the changes made here could be revert in the following method, which will be invoked before the app is shown to you on the screen.
-(void)applicationWillEnterForeground:(UIApplication *)application{
}
Upvotes: 0
Reputation: 118
Subscribe to the notification UIApplicationWillResignActiveNotification. As a response to this you can update your UI.
Upvotes: 0
Reputation: 2399
try with enablingt Background task Identifire
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
UIBackgroundTaskIdentifier bgTask;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
[splitVO dismissViewControllerAnimated:true completion:nil];
}
Upvotes: 0