This post is for Adobe Flex/Actionscript/Flash developers who are looking to build commercial-grade mobile apps. I‚Äôve tried to¬†pull together¬†a high-level check list¬†of items¬†you‚Äôll need to build successful and stable apps based on Flex 4.5.1. I‚Äôve also uploaded a fully-functional prototype that demonstrates these concepts in a real-time, GPS navigation app. You can download the app here. So, here goes.
1. Set your initial splashScreenImage and application icon. For your app to look professional you’ll want to display an image while it launches so there isn‚Äôt just a blank screen. Here‚Äôs a great blog post that goes into more detail and covers handling multiple screen resolutions. One caveat on the splashScreenMinimumDisplayTime property is use this with caution. If you delay the app start too much you run the risk of really annoying users.
And, be sure to set the application icon. When you install your app, this is the image that will be displayed in the phone‚Äôs UI. Configure this in the yourappname-app.xml file. Note if you image icon isn‚Äôt the absolute correct size you‚Äôll get a compiler error:
2.¬†Manage your applications life-cycle. The best article to read is the old but still very useful Hero View and ViewNavigator¬† – Functional and Design Specification and this blog post on Understanding View and ViewNavigator. For some reason the ViewNavigatorEvent poperties listed below aren‚Äôt documented in the Adobe on-line help. I‚Äôve complained and so should you!
- viewActivate Event ‚Äď called when the view is fully activated. It actually happens after the creationComplete event. If you want to know more about view states in general then read this Adobe article.
- viewDeactivate Event ‚Äď use this in a View if you want to handle certain things when the user changes to a different View and the current¬†one has been deactivated.
- removing Event ‚Äď This is called right before the viewDeactivate Event. So if there is something you want to do right before the view is fully deactivated then use this event.
- persistNavigatorState ‚Äď This property works at the application level and allows you to save the navigator‚Äôs view stacks and navigation history to a local persistent object. This is a property that is set in the main application‚Äôs mxml file and by default it is set to false. The standard architecture of a mobile app is to destroy the view contents when a user switches views so that the application saves memory. But, if there is a significant cost to destroying and recreating a particular view then you should test setting this property to never. Cost in this case means the amount of time, memory and CPU it takes to destroy and recreate a view. Also, if your end user is repeating this over and over that will ultimately affect battery life. Once a view is destroyed my guess is that memory is set for garbage collection. For info see this very informative Adobe blog post.
- destructionPolicy ‚Äď This is a property that can be set on individual views and can prevent an individual view from having all its data destroyed when the view is deactivated. For example, you may allow some views to be destroyed where others are mission critical and shouldn‚Äôt be destroyed because it‚Äôs too expensive to recreate them. As I write this, I believe this only works if the persistNavigatorState property has been set but it‚Äôs been a while since I verified that.
3. Manually changing views. Use pushView(), popView(), popToFirstview(), popAll() and replaceView().
- pushView() navigates the user to a new screen.
- Use popView() to move back to the previous screen.
- popToFirstView() changes to the screen to the very first view that was opened. This is programmatically referred to as the view at the bottom of the view stack and uses the FIFO principal.
- popAll() returns a blank screen. I‚Äôve never used this and I haven‚Äôt come across a use case (yet) that would require given the user a blank screen.
- replaceView()¬†removes the current view and replaces it in the view stack with the new view you that you assign.
4. Passing data between views. One of the requirements of commercial apps is sharing data between different views. There are a number of ways to do this including singletons, dependency injection and using the data property in the pushView() method. Here are some good articles on all three:
- Using singletons or tightly coupling data. This is typical for prototyping where you don‚Äôt want or need the overhead of a full framework. The prototype app download (link at top of page) uses a singleton model for simplicity.
- Using framework-based, dependency injection. Use this when you want to use a framework such as Swiz, Parsely or Robotlegs.
- Using the pushView() data property. When you have fairly simple data needs use this via the pattern pushView(viewClass:Class, data:Object = null, context:Object = null, transition:spark.transitions:ViewTransitionBase = null) Note that this pattern is for basic usage and the data object only supports standard content within the object such as Strings, Array, ArrayCollection, etc. If you have a custom class be sure to register them with the registerClassAlias() method or you‚Äôll get runtime errors when you go to switch views.
5. Set application permissions.These are root permissions that are set via manifestAdditions for Android and infoAdditions for iOS ‚Äď and these are located in the yourappname-app.xml file in your application‚Äôs root directory. Here‚Äôs an Adobe article with additional details. When the application is installed the user will be alerted to what permissions you are asking for.
<!--See the Adobe AIR documentation for more information about setting Google Android permissions-->
6. Shutdown the app. This only works on Android. On iOS, the user has to do this manually.
7. Temporarily disable the screen saver. This is required in apps where you don‚Äôt want the screen to go to sleep such as navigation apps where it may be open for a long time without any user intervention. You also need to set the WAKE_LOCK permission in the manifest file.
//Make sure we are on a mobile device and then
//keep the application awake so it doesn't go to sleep and close the screen.¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†
if(Capabilities.cpuArchitecture == "ARM")
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†
8. Detecting when phone rotates. If you need to know when the phone rotates use this listener:
9. Gracefully fail when network connection is lost. If your app needs network access then it‚Äôs a best practice to gracefully fail and let the user know when internet connection is lost and then again when it‚Äôs restored.
public function NetworkChangeController(autoStart:Boolean = false)
var req:URLRequest = new URLRequest(_MAP_URL);
_urlMonitor = new URLMonitor(req);
private function networkChangeHandler(event:Event):void
private function serviceMonitorStatusHandler(event:StatusEvent):void
trace("Network Status Event: " + event.code + ", " + _urlMonitor.available);
¬†¬†¬†¬†¬†event.code == "Service.unavailable" ? _doSomething = false : _doSomething = true;
10. Multiple Device Support ‚Äďsizing for different dpi‚Äôs. Last, but not least is using CSS and media queries to help with sizing and layout. Media queries are actually part of the W3C core CSS spec. The cool thing about them is they let you auto-majically detect the users screen dpi (dots-per-inch) and operating system and adjust your CSS accordingly. This saves a huge amount of work on your part:
@namespace s "library://ns.adobe.com/flex/spark";
/* DPI specific styles */
/* Platform specific styles */