PlayBook: Application Lifecycle

Posted:   |  More posts about PlayBook ActionScript3

On the PlayBook an application goes through several stages in its lifecycle, and various events are issued to allow an app to monitor its externally controlled state. I say "externally controlled" because these state changes are generally out of the control of an application.

When an app is first launched, the NativeApplication.nativeApplication object will get an InvokeEvent.INVOKE event. This is likely not of much use, as an application knows it was just invoked when it enters its main constructor routine (generally a subclass of flash.display.Sprite).

When the application first becomes visible and fills the screen, where it's considered to be really running, an Event.ACTIVATE event will be sent and can be captured on the main Sprite. This same event is sent whenever a previously minimized app is restored, or when side-swiping changes to your application from another one.

An Event.DEACTIVATE is sent when the user switches away from your application, either by side-swiping or by swiping up from the bottom bezel to minimize the application in the "shuttle bar".

The call NativeApplication.nativeApplication.addEventListener(Event.EXITING, callback) can be used to subscribe a callback routine to an event sent when the user closes your application, either by clicking on the X button in the upper right of the minimized app's window, or by dragging it up from the shuttle bar to close it. This event is also sent when your application calls stage.nativeWindow.close().

Because you can't call event.preventDefault() on the Event.EXITING event, you can't prevent your application from closing, it appears. For that reason and probably others, it's definitely best to try to preserve all important state in response to a DEACTIVATE event, so that if the user subsequently closes your app you have already saved.

Lastly, though perhaps of no more use than EXITING, you can call stage.nativeWindow.addEventListener() with Event.CLOSING to hear about a CLOSING event that is sent after the EXITING event. It also is not cancellable with preventDefault(), even though both report true on their cancelable properties.

[Edit: Note that if the PlayBook is set for "Showcase" mode, where apps are left fully running even when you switch away from them, you may never receive a DEACTIVATE event. You shouldn't rely solely on this event to signal that it's time to save your state.]

Comments powered by Disqus