Activate/Deactivate event doesn't fire

Hi there,

I want to know when the user switch out or back to the app.

The documentation says

  • Activate “occurs … or when the user switches back to the app”.
  • Deactivate “occurs … or when the user switches out of the app”.

I tried, in simulator and on a real device, with a test app and a single View:

  • pressing Home button doesn’t fire the Deactivate event
  • opening the app again doesn’t fire the Activate event

The Activate event fires only once when the app is first launched.

Is it a bug or a misunderstanding ?

Thanks !

[quote=162258:@Olivier Colard]Hi there,

I want to know when the user switch out or back to the app.

The documentation says

  • Activate “occurs … or when the user switches back to the app”.
  • Deactivate “occurs … or when the user switches out of the app”.

I tried, in simulator and on a real device, with a test app and a single View:

  • pressing Home button doesn’t fire the Deactivate event
  • opening the app again doesn’t fire the Activate event

The Activate event fires only once when the app is first launched.

Is it a bug or a misunderstanding ?[/quote]

When you press Home, the app is not quit. It is simply “frozen” in its current state. You can come back to it by pressing Home twice, like a double click. You will get a list of all the current apps and be able to switch between them.

There are events at the system level that tell if the app is active or not.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html

applicationWillResignActive: is when the app is about to deactivate
applicationDidBecomeActive: is when the app activates

Hi Michel,

Thanks for your answer. I’ll check the Apple doc.
But then I don’t see what Xojo mean by “when the user switches out/back to the app” ?

I guess I’ll be able to use DeclareMaker to generate the classes but I don’t really see how to handle system events.

Thanks anyway for putting me on the track.

<https://xojo.com/issue/37884>

Hi Oliver,
DeclareMaker won’t be able to help you with capturing delegate methods like “applicationWillResignActive:” to do that you would need to create a dynamic NSObject subclass using the objc runtime. I think that doing this may break some of the things which Xojo does with event handling however (low memory warning, etc), so the best route is a feature request to add handling of those events to the App class.

FYI I filed a feedback case for Application.WillEnterForeground some time ago: <https://xojo.com/issue/36998>.

Thanks for your answers.

I’ve solved this issue with a xojo.Core.Timer
The timer will remember each 10 seconds a timestamp.
When the app is gone to the background this timer is frozen by iOS.
When the app is selected by the user again the timer will run again and sees that it is some ‘long’ time ago when it looks to Date.Now and the last timestamp and will call some things I want to refresh.

The documentation has been corrected.

Also, I had a look in SO about that, and it seems possible to get the event in the ViewController without the delegate :

[quote]The easiest way to handle this is to register to receive the UIApplicationWillResignActiveNotification notification in your view controller.

The event is issued upon a home button press, lock and upon a phone call[/quote]

[code]- (void) applicationWillResign{
NSLog(@“About to lose focus”);
}

  • (void) myVcInitMethod {
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(applicationWillResign)
    name:UIApplicationWillResignActiveNotification
    object:nil];
    }[/code]

Jaap, it’s a good idea. I already have a timer to check for a timeout at app level. I’ll create a second one.
Michel, thanks.

The timer is a good way to detect when the app becomes active again. But I see no Xojo method to detect the home button press, which can be used for housecleaning.

The classic case of using window.close or app.deactivate is to close a db or save preferences or data. Since that is not available, I guess one will have to close the db after each use, and save preferences or data whenever the user is inactive.

You are right
There are a lot more situations than on desktop that can disactivate or even halt your app.
So save your data as soon you can.
Make your data easily reloadable

I just had an idea on how to do this without breaking Xojo event handling, and have successfully implemented the applicationWillResignActive delegate method so that we can receive an event when the home button is pressed. I’m going to add a few more important (at least in my eyes) delegate methods, then I’ll post the project. If there are any specific delegate methods which you would like to see added, please post them and will add them to the class.

Splendid news :slight_smile:

I suppose you have already implemented applicationDidBecomeActive that the OP wanted as well ?

Of course :slight_smile:

I was going to add some of the delegate methods that deal with notifications but that is going to take additional work in creating a few separate classes so I’m actually going to leave it for a later time.
Here is the project:
https://www.dropbox.com/s/ornbgst9rmme6t7/improved%20iosapplication.xojo_binary_project?dl=0
To use simply add the class to your project and change the super of the App object in your project from iOSApplication to ImprovedIOSApplication. Then implement the events you want and that’s it.
Enjoy!

[quote=163679:@Jason King]I was going to add some of the delegate methods that deal with notifications but that is going to take additional work in creating a few separate classes so I’m actually going to leave it for a later time.
Here is the project:
https://www.dropbox.com/s/ornbgst9rmme6t7/improved%20iosapplication.xojo_binary_project?dl=0
To use simply add the class to your project and change the super of the App object in your project from iOSApplication to ImprovedIOSApplication. Then implement the events you want and that’s it.[/quote]

Jason, that is splendid !

Thank you so much :slight_smile:

Great !!
Jason, you’re wonderful !

Is there also an event for receiving an APN?

Yes, I’ll be doing something similar to capture notifications.