Detect application refocus event
cordova-2.0.0, ipad, javascript, sencha-touch
Solution
An ios-app developper accepted to help me. His answer suits me very well, as it seem clean et reusable. So I will produce it here :
The "app come to foreground" event can be catched through the applicationWillEnterForeground event. Phonegap/Cordova allows to call javascript functions through the Cordova classes. The webView object has a dedicated method to launch js scripts.
So I opened the file Projet/Classes/Cordova/AppDelegate.m :
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"applicationWillEnterForeground: %@", self.viewController.webView);
[self.viewController.webView stringByEvaluatingJavaScriptFromString:@"notifyForegroundEvent();"];
}
And I added the notifyForegroundEvent() method somewhere in the root of my js files :
var notifyForegroundEvent = function() {
console.log('notifyForegroundEvent()');
// Do something here
}
Et voilà
Problem
I need to know each time the user is starting to use the application. Case 1 (OK) : The app is not started. The user starts the application from scratch. I add a listener on the app's bootstrap, and I am aware when it starts. Case 2 (TODO) : The app has been started but it's not active anymore The user reload the application from the taskbar. I want to know when the application comes from the taskbar to the foreground (like a alt+tab). This is tricky to catch, because the app is still running and I don't know which event to listen to. In fact, I don't even know how to name this behaviour.