Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell my app is about to become inactive/go to background state?

I am assuming I need to implement:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(resignActive:)
                                                     name:UIApplicationWillResignActiveNotification
                                                   object:nil];

but am not sure if this is the right notification to determine my app is about to leave active state.

Is this a good place to cancel network connections, along with app termination?

like image 884
Sheehan Alam Avatar asked Jul 29 '10 16:07

Sheehan Alam


People also ask

What happens when app goes into background?

When an App is put on background and then resumed, it resumes the specific Activity it was in before going to background. This means that you would need to implement whatever you want done on resuming from background in all Activity of your Application.

What is inactive state in iOS?

The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state.


1 Answers

There are cases where UIApplicationWillResignActiveNotification is sent but the application does not enter the background, for example if a call is received but the user chooses not to answer it.

Use UIApplicationDidEnterBackgroundNotification to be notified when entering the background. Be aware that this will sometimes be sent after UIApplicationWillEnterForegroundNotification if the application is quickly opened again.

like image 145
drawnonward Avatar answered Oct 13 '22 23:10

drawnonward