Native extensions: When to call FREContext instance methods

Here’s a little tidbit about developing native extensions for Adobe AIR for Android devices using the native extension Java API.

The main point: The call that your extension’s ActionScript side makes to ExtensionContext.createExtensionContext()  must return before the native side of the extension can call methods of the instance of your class that extends the  FREContext class.

Let’s break that down.

The AIR runtime instantiates your FREExtension instance the first time the  ActionScript side of your extension calls ExtensionContext.createExtensionContext().

The sequence of calls to your Java implementation of the FREExtension class is:

  • FREExtension implementation class constructor
  • initialize()
  • createContext()

In createContext(), you return an instance of your class that extends the FREContext class.

But be careful. The FREContext object can only be considered fully constructed once the ExtensionContext.createExtensionContext() ActionScript method has returned.

Therefore, do not call any methods of your FREContext instance until after the ExtensionContext.createExtensionContext() ActionScript method has returned.

For example, you might be tempted to call your FREContext instance’s getActivity() method in its constructor. Don’t do it. You’ll get an IllegalStateException if you do.

Instead:

  • Provide an initialization method that the ActionScript side can call after ExtensionContext.createExtensionContext() returns.
  • In the initialization method, call your FREContext instance’s getActivity() method.

The Gyroscope and Vibration examples do exactly that. See them at http://www.adobe.com/devnet/air/native-extensions-for-air.html.

Detailed documentation about creating extensions is at Developing native extensions for Adobe AIR.

To use a native extension in your AIR application, see Using native extensions in AIR applications.