Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between applicationDidEnterBackground and applicationWillTermimate

In my application when user hits home button I save the data in NSUserDefaults. Application is running in background. When user re-launches the app I use the method applicationWillEnterForeground to display the saved data.

However when user double taps on home button and quit the application by selecting minus sign on the app, I need to save different data in that same user defaults. But when I quit the application applicationWillTerminate is called sometimes and sometimes not.

So, how can I differentiate that the application is just minimized or it is quit?

like image 505
Prerna chavan Avatar asked May 07 '12 05:05

Prerna chavan


3 Answers

Whenever user taps one time only and application will go in background at that time applicationDidEnterBackground always calls. In this method you can temporarily update/store your NSUserDefaults value.

If in case, application will again come in foreground with out terminating by using double taps, applicationDidBecomeActive will call, in which you can again update/remove temporarily stored NSUserDefaults value. And if user quits application by double taps, the stored value will be kept with NSUserDefaults.

like image 139
alloc_iNit Avatar answered Sep 24 '22 17:09

alloc_iNit


About the difference among the UIApplicationDelegate methods, please refer to UIApplicationDelegate Protocol Reference. applicationDidEnterBackground: will always be called when the home button is pressed. But it seems that applicationWillTerminate: is not guaranteed to be called.

If your app is not running in foreground, it can be either in background mode or in suspended mode. Please refer to "The App Launch Cycle" section of iOS App Programming Guide - App States and Multitasking.

When the app is running in background and monitoring events, applicationWillTerminate: will be called when it is terminated. When the app is suspended, it will not. See the description in "App Termination" section:

Even if you develop your app using iOS SDK 4 and later, you must still be prepared for your app to be killed without any notification. The user can kill apps explicitly using the multitasking UI. In addition, if memory becomes constrained, the system might remove apps from memory to make more room. Suspended apps are not notified of termination but if your app is currently running in the background state (and not suspended), the system calls the applicationWillTerminate: method of your app delegate. Your app cannot request additional background execution time from this method.

like image 30
Hailei Avatar answered Sep 22 '22 17:09

Hailei


applicationDidEnterBackground - Whenever user press home button and application will go in background at that time it call applicationDidEnterBackground method. It gets called whenever the user minimizes the application by pressing the Home button or by switching to another application.

applicationWillTerminate - It called only when the applications' process is actually killed. That is, the user kills it using the red minus close button in the task switcher, or the system itself kills it while in the background (for example on a low memory condition). Your app cannot request additional background execution time from this method.

like image 38
Chetan Bhalara Avatar answered Sep 24 '22 17:09

Chetan Bhalara