Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

applicationDidBecomeActive getting called twice

My app delegate method applicationDidBecomeActive: is getting called twice for the first time launch of the application. I have some portion of code which I want to execute only once & that I have put into applicationDidBecomeActive:

What should I do?

like image 647
Abhinav Avatar asked Apr 24 '12 19:04

Abhinav


8 Answers

I got the issue. I am using Location Services. When launching for the first time after I tap on "OK" on the location services alert, my applicationDidBecomeActive gets called one more time which is the normal iOS behavior.

like image 103
Abhinav Avatar answered Oct 11 '22 00:10

Abhinav


If you want to call your code only once when app becomes active, try calling it from two methods.

  1. didFinishLaunchingWithOptions
  2. applicationWillEnterForeground

instead of calling it only from applicationDidBecomeActive.

like image 30
tejashri Avatar answered Oct 10 '22 22:10

tejashri


This is because of location or push notification alert.

After the native location/push notification has been dismissed, applicationDidBecomeActive will be called.

like image 38
Jingwei Avatar answered Oct 10 '22 23:10

Jingwei


I don't know if this will help, but I just had the same issue with a totally simple app that doesn't use Location Services, and I found out it's an illusion. Look at the logging messages I got:

2012-12-22 10:47:45.329 Bizarro[10416:907] start applicationDidBecomeActive:
2012-12-22 10:47:45.333 Bizarro[10416:907] end applicationDidBecomeActive:
2012-12-22 10:47:45.329 Bizarro[10416:907] start applicationDidBecomeActive:
2012-12-22 10:47:45.333 Bizarro[10416:907] end applicationDidBecomeActive:

Look closely. Look at the times. The first and third messages have the same time. The second and fourth messages have the same time. They are the same messages! It's an Xcode bug; it has nothing to do with my code. Xcode is reporting the same log messages twice.

In my case, I was able to prevent this by turning off all Behaviors for Running -> Generates Output.

like image 31
matt Avatar answered Oct 10 '22 23:10

matt


What about:

  • Increment on applicationDidBecomeActive
  • Decrement on callback events of permissions requests or other alerts that trigger another applicationDidBecomeActive when closed.
like image 28
Gabriel Jensen Avatar answered Oct 11 '22 00:10

Gabriel Jensen


With Xcode 6 there's a new reason this can happen: when you launch an app in a resizable simulator, applicationDidBecomeActive: will get called twice.

It launches the app with the default size class, and then applies the size you were last using—even if you were using the defaults. Any time a change in size class is applied, applicationDidBecomeActive: gets called.

like image 32
robotspacer Avatar answered Oct 11 '22 00:10

robotspacer


When app launches first time it calls sequentially,

  1. didFinishLaunchingWithOptions
  2. applicationDidBecomeActive (Twice)

When we open the Control Center it calls only,

applicationDidBecomeActive

When app come from background to foreground it calls sequentially

  1. applicationWillEnterForeground
  2. applicationDidBecomeActive
like image 20
Shuvo Joseph Avatar answered Oct 10 '22 22:10

Shuvo Joseph


I'm currently seeing double notifications.

It's happening because my AppDelegate's init code is being called twice.
It's being called once when the main() does [[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:application topLevelObjects:&tl] (ie, when the .XIB file is loaded), becaues the .XIB file is setting up FirstResponder to my custom AppDelegate, and then it's being called again when main() calls [[myAppDelegate alloc] init].

The init code is what does the addObserver calls, so two observers are being set up for each notification I care about, which is why my notifications get called twice.

I'm a newbie OS X programmer, so I'm not yet sure of the best way to resolve these two, but wanted to post it here in case it's of help to others, another place to look.

like image 22
Everett Kaser Avatar answered Oct 11 '22 00:10

Everett Kaser