Is applicationDidEnterBackground ALWAYS called before applicationWillTerminate?
Asked Answered
L

1

7

Is applicationDidEnterBackground ALWAYS called before applicationWillTerminate in an iOS app? I know that applicationWillTerminate is not always called (multitasking) - but when it is called, is applicationDidEnterBackground ALWAYS called first? I don't want to duplicate code unnecessarily by including it in applicationWillTerminate if it is already included in applicationDidEnterBackground, for an app that supports multitasking.

Lop answered 5/12, 2012 at 16:28 Comment(4)
If you are multitasking, and the app entered background, why should it go trough applicationDidEnterBackground again before its killed by user ?Rosalia
It was my understanding that if a user kills an app that is running in the background by double-tapping the home button and tapping the red minus button that applicationWillTerminate is not called. Let me know if I am wrong.Lop
@Rosalia Maybe a good question to ask is when IS applicationWillTerminate called for an app that supports multitasking..Lop
I found this on another site:"there is one instance on a multitasking device where applicationWillTerminate is called: If the app is in the foreground and you power off the device. In that case, both applicationDidEnterBackground and applicationWillTerminate get called." cocos2d-iphone.org/forum/topic/7386Lop
A
6

in ios 4.0 and later applicationDidEnterBackground is called instead of applicationWillTerminate so you don't have to call both of them. Here is the portion of the Apple docs:

Discussion

In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution. You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. You should also disable updates to your application’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your application is terminated and purged from memory.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

The application also posts a UIApplicationDidEnterBackgroundNotification notification around the same time it calls this method to give interested objects a chance to respond to the transition.

For more information about how to transition gracefully to the background, and for information about how to start background tasks at quit time, see iOS App Programming Guide.

Hope this helps clear the issue for you man. Adrian

Here is the link to the technical note that is available on developer section. It is dealing with networking and multitasking. The actual method used in this doc deals with only applicationDidEnterBackground and since iOS 5 they have a system called watchdog which terminates the app if the network is unresponsive automatically. Hence there is no need to actually call applicationWillTerminate and try to execute codes to allow your app to finish its task before the app is terminated. The app will enter the background and will continue its task until the last task is completed. I hope that makes sense, but here is the link. Please read the watchdog section. https://developer.apple.com/library/ios/#technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841

Hope this helps. :)

Apomixis answered 5/12, 2012 at 16:45 Comment(9)
Thanks, I did understand that, and I have all necessary code in applicationDidEnterBackground. I am just trying to figure out which code to include in applicationWillTerminate and which to leave out.Lop
I do really appreciate the comment on timing out of applicationDidEnterBackground.Lop
i don't even call anything in application will terminate method anymore. i just leave it as default that comes up when creating the necessary views. as of ios 5 i have been calling all the methods in application did enter background and it works just fine for me. i don't know of any other that is done.Apomixis
It seems as if applicationWillTerminate is only called for multitasking when "the app is in the foreground and you power off the device. In that case, both applicationDidEnterBackground and applicationWillTerminate get called." (according to user reply in cocos2d-iphone.org/forum/topic/7386). If that is true, then it is not necessary to duplicate any code from applicationDidEnterBackground in applicationWillTerminate for multitasking apps (as you have said).Lop
yes you are right about that. if my answer was of any help to you will you check my answer as the correct one please.Apomixis
Yes, I understand. Before I accept an answer, I would like to confirm the statement from the cocos2d forum (in my comment above) from the Apple Docs officially. If you know where it states that, please add that to your answer, or I will add a comment if I find it.Lop
Cool, let me look a bit more and in the meanwhile I'm sure one of the guys in cocos2d maybe answering that in that forum as well. Good luck and happy coding.Apomixis
According to this link, "There is one situation where applicationWillTerminate is still called. Applications that actually continue to run in the background because they have requested extra time or access to the background VOIP, audio or location services can be terminated by the system and will receive a call to applicationWillTerminate." useyourloaf.com/blog/2010/07/04/… In this case, applicationDidEnterBackground would have already been called. In any case, it does seem that it is not necessary to duplicate code in the two methods.Lop
Amp,I am accepting your answer, but please do add the Apple doc info if you find it. I did a short look around and could not, but I'm sure it's out there somewhere.Lop

© 2022 - 2024 — McMap. All rights reserved.