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?

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.


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.


This is because of location or push notification alert.

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


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.


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.


What about:

  • Increment on applicationDidBecomeActive
  • Decrement on callback events of permissions requests or other alerts that trigger another applicationDidBecomeActive when closed.

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

Have you possibly created an instance of your class in Interface Builder AND in your AppDelegate code, perhaps?


If you have code you want called only once when the app starts up, then use

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

instead.

Otherwise, applicationDidBecomeActive will be called whenever your app becomes active again, so that doesn't just mean twice as in your case, but every time the user switches back to your app after switching to another.


If you use RxSwift, then you can just throttle the application event, so it doesn't call twice in the same second:

NotificationCenter.default.rx.notification(Notification.Name.UIApplicationDidBecomeActive)
        .throttle(1, scheduler: MainScheduler.instance)
        .subscribe { (event) in
            self.appEnteredFromBackground()
        }.disposed(by: disposeBag)

private func appEnteredFromBackground() {
        Analytics.trackPageView(withScreen: .home)
        dataStore.checkIfAllowingRides()
    }

I put this code in my actual view controller where the logic is supposed to happen on ApplicationDidBecomeActive.


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.


Need Your Help

How i can use the connectionString of the current website for log4Net instead of configuring

c# log4net connection-string

I use log4Net for my system's log. The connectionString node is mandatory if the appender type is the AdoNetAppender in Log4Net. However, I already have a connectionString in my website where I use

How do I have a breakpoint get triggered if an instance variable in the class has its value changed?

ios objective-c xcode lldb

Say I have a variable, self.position, how do I get Xcode to break whenever it changes its value (a number of methods could change it).