Skip to content

Instantly share code, notes, and snippets.

@alunny
Created July 26, 2010 23:50
Show Gist options
  • Save alunny/491468 to your computer and use it in GitHub Desktop.
Save alunny/491468 to your computer and use it in GitHub Desktop.
appActive event for phonegap-iphone
// insert this into your AppDelegate.m file
// this will fire an "appActive" event on the webview every time the app is launched
// whether through fast-app switching or a cold launch
- (void)applicationDidBecomeActive:(UIApplication *)application {
// delay of 1ms ensures code will be executed in new stack trace
// that way, event listener can't block applicationDidBecomeActive
// and crash the app
NSString *fireActiveEvent = @"window.setTimeout(function() { \n"
"var appActive = document.createEvent('Events'); \n"
"appActive.initEvent('appActive'); \n"
"document.dispatchEvent(appActive); \n"
"}, 1);";
[webView stringByEvaluatingJavaScriptFromString:fireActiveEvent];
}
@purplecabbage
Copy link

For simplicity, I recommend we make all events lowercase, we ran into issues with deviceReady vs deviceready
Otherwise, super cool!

Ultimately, we need events for all of these:

– application:didFinishLaunchingWithOptions:
– applicationDidBecomeActive:
– applicationWillResignActive:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
– applicationWillTerminate:
– applicationDidFinishLaunching:

@alunny
Copy link
Author

alunny commented Jul 27, 2010

Agreed on lowercase

The android lifecycle methods are:
     protected void onCreate(Bundle savedInstanceState);
     protected void onStart();     
     protected void onRestart();
     protected void onResume();
     protected void onPause();
     protected void onStop();
     protected void onDestroy();

There's some crossover there - I think onResume is the closest equivalent to applicationDidBecomeActive

@orbitaloop
Copy link

So useful, we need that by default in Phonegap !

@simme
Copy link

simme commented Jan 11, 2011

I agree, this should totally be in PhoneGap by default! Why isn't it yet? :)

Any tips on how I can accomplish the same as the above gist on Android? I'm not that good with Java :P

EDIT: Figured it out: https://gist.github.com/9de0b38205ee62b43dfe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment