Understanding Flex Mobile View and ViewNavigator
The Flex framework “Hero” makes the task of creating mobile applications for BlackBerry Tablet OS, Android, and soon iOS extremely simple. I think that one of the best looking apps for Android is MAX Companion 2010 and please bear in mind that this app was created using an early build of Flex “Hero”.
This is a reasonably complex application with lots of screens and social integration (Twitter) and yet on my Nexus One and Samsung Galaxy Tab it still works great. This is exactly where the Flex framework can shine on mobile devices: fast and easy development of applications that can connect to virtually anything (web services, REST services, RPC).
In this article I will talk about Flex Mobile Views and ViewNavigator. If you want to use Flex “Hero” to create mobile apps you need to understand how these components work.
Oh and one more thing, if you want to try this yourself you’ll need the Flash Builder “Burrito”. You can download it from here.
Screen Metaphor
Before talking about Flex mobile components let’s talk a little bit about a concept called screen metaphor. This is basically a neat solution for a very big problem any developer encounters when developing for mobile devices: screen real estate.
On a desktop computer we have enough resolution to fit all the information we want on the screen. By using hierarchical menus and pop-up windows we can push a lot to the screen. Compared to this, a smartphone screen feels (almost) like a stamp. Besides resolution, pixel density plays an important role – my Nexus one has a resolution of 480X800 pixels and 254 pixels per inch. This means that you have to enlarge the text roughly two to four times compared to the same resolution on a desktop/laptop screen when building apps for mobile devices. Thus you end up with less space to display information.
Using the screen metaphor you can split the information/UI of your application on multiple screens. For example when a user wants to see details for a particular session (see the above pictures of MAX Companion 2010 app) another screen is presented. When he wants to go back to the session list, the previous screen is presented.
Elegant though it might be, the screen metaphor is not rocket science. However there are some things to be considered such as:
- memory management – pushing dozens of screens can eat up all the memory available on the device
- transitions from one screen to another – those nice effects that help you create an application like a professional UX Designer
- passing data from one screen to another
- preserving application state when the OS choose to close it
- integration with hardware buttons – for example using the Back button of the phone to navigate to the previous screen
Fortunately, the Flex framework has built-in support for the screen metaphor so you don’t have to reinvent the wheel.
Flex Mobile Project
When you install Flash Builder “Burrito” you will notice that there are two new types of projects related to mobile: Flex Mobile Project and ActionScript Mobile Project.

If you want to use Flex support for mobile development you should choose Flex Mobile Project and on the second page of the wizard make sure you select Mobile Application (this option makes a project that has uses Flex built-in support for the screen metaphor). As you can see in the screen capture below, you can choose to target both BlackBerry Tablet OS and Google Android platforms or just one of these platforms (as I said before we are working to extend the reach of the Flex framework to iOS – sometime in 2011).

Once you click Finish you will have a project structured like this:

Compared to a desktop AIR project you have one extra file: views/MobileProjectHome.mxml. The other two files are the “usual suspects”: the main application file (MobileProject.mxml) and the application descriptor file (MobileProject-app.xml). So what you have here is a skeleton for a Flex mobile application that has a single screen/View for now: MobileProjectHome.mxml. And if you open the main application file (MobileProject.mxml) you’ll see that on the MobileApplication tag there is an attribute named firstView and its value is set to views.MobileProjectHome – this is how you set the default screen for your mobile app.

Now, I’d like to step back and tell you about the two options you have when building Flex mobile apps. The first one is to use MobileApplication like in the project above. However if you work on an application that has lots of screens, it might be a good idea to use TabbedMobileApplication. As the name suggests you’ll have an application with support for tabs and for each tab you can have a stack of screens/Views (for each tab you can define the default/first screen). Here is an example:

And here is the source code for the main application file:

Views and ViewNavigator
Now let’s dive deeper and see how you can use Views and ViewNavigator to build a mobile application. First of all, screen metaphor support is built-in in the MobileApplication/TabbedMobileApplication and ViewNavigator. You use View components to create the UI of your application.

If you want to add a second screen to your Flex mobile application you will create a new MXML component inside the views package (this is the folder created by new project wizard; of course you can choose to place the views in a different package) and this new component must be based on spark.components.View.

The spark.components.View class extends the Group component and is familiar if you have worked previously with Flex 4. For example here is the source code for a second screen (SecondScreen.mxml) that uses VerticalLayout to lay down two components: a Button and a TextArea component:

And here is what the screen looks like on a device:

When you want to set a title for a screen you use the title attribute of the View component. What about changing the current View (screen) and moving to another one? Well, here comes in the ViewNavigator component. The View component has a property called navigator (it represents the instance of ViewNavigator that manages the Views). Using this property you can push a new View (screen), move back to the previous View, jump to the first View and more:
- navigator.push(SecondScreen, data) – move to a new screen
- navigator.popView() – move back to the previous screen
- navigator.popToFirstView() – jump to the first screen
- navigator.activeView – returns the active view
Going back to our project, if you want to move to the second screen we’ve created earlier (SecondScreen.mxml) you can do something like this in the first screen (MobileProjectHome.mxml):
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
title="Home">
<fx:Script>
<![CDATA[
private function goToSecondScreen():void {
navigator.pushView(SecondScreen);
}
]]>
</fx:Script>
</s:View>
What about moving back from the second screen to the first screen? Your application is automatically wired to listen to Back hardware button. Thus, if the user presses the Back button he will go to the previous screen. Or you can add to your application a software button that executes this code when pressed: navigator.popView().
If you want to pass some data from one view to another (for example an ArrayCollection or some other Data Model), then you can use the second argument of the navigator.pushView() method:
navigator.pushView(SecondScreen, myData);
You can access the data pushed by the previous View in the current one by accessing the data property of the View class.
ActionBar
There are situations when you want to have at the top of your screen some sort of a bar that can be used for:
- a title customized for the current information displayed
- a navigation button to go back to the first screen
- buttons for actions related to the current screen
Maybe you need something like this:

Again Flex comes to your rescue. For each view you can specify ActionBar content. This bar has three different areas: navigation content, title content, and action content. You can make use of any or all of them. Here is the code to add two buttons and a title to a View using the ActionBar component:
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> <s:navigationContent> <s:Button label="Back"/> </s:navigationContent> <s:titleContent> <s:Label text="This Title Vies"/> </s:titleContent> <s:actionContent> <s:Button label="Opt"/> </s:actionContent> </s:View>
And here is the screenshot:

Some notes on ActionBar usage:
- You can control the ActionBar visibility using the actionBarVisible View property. You can change this property at runtime
- You can overlay the ActionBar on top of the View content using the overlayControls View property
- You can set the ActionBar content in each View or, if you have some controls that are used across different Views you can set them up in the main application file. Then you can overwrite just the bits you need to be different in each View. If you add a Back button in the main application file, then all views will have this button. However, if you decide the first View doesn’t need a Back button, you can override this by setting navigationContent to null in the first View:
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> <s:navigationContent/> </View>
- Although, you can control the ActionBar content from any View or from the main application file, in fact the content is managed by the ViewNavigator component
Views life cycle
Now let’s talk about the Views life cycle. There are three important events for any View that you should be aware of:
- viewActivate (FlexEvent.VIEW_ACTIVATE) – Dispatched when the current view has been activated.
- viewDeactivate (FlexEvent.VIEW_DEACTIVATE) – Dispatched when the current view has been deactivated.
- removing (FlexEvent.REMOVING) – Dispatched when the screen is about to be removed in response to a screen change. If you called preventDefault() you can cancel the screen change. This event is triggered before the FlexEvent.VIEW_DEACTIVATE event.
A question that might bother you is what is happening with a View when the FlexEvent.VIEW_DEACTIVATE event is triggered? Well, the default behavior is that the view is destroyed. This is great because it keeps the memory foot print small as you push and pop Views.


This raises two more questions. What happens to the data you might have in a View when the View is destroyed? And, can you prevent a particular View from being destroyed? If you want to preserve the data you use in a View all you have to do is to assign those data to the View.data property. Suppose you read some data by calling a web service. Because this information doesn’t change too often and it takes a while to get it from the Cloud you want to have the data saved. All you have to do is to assign the data you get from the web service to the View.data property.
If you want to prevent the entire View from being destroyed (maybe it is a complicated View and it takes too much to create it) you can use View object’s destructionPolicy attribute: destructionPolicy=”none”.
What about making sure that the entire application state is preserved in the case the application is closed by the OS? All you have to do is to set the sessionCachingEnabled attribute in your main application file to true. This is a neat feature that allows you to support (with no sweat) scenarios like this: the user navigates to the n screen of the application, he has some data on that screen and the application gets closed (maybe the user went to a new application or decided to close the app); when he opens the application again he will see the same screen as in the previous session.
The last point I want to make is related to the splash screen feature. If your application is not trivial then it will take some time to load. Instead of letting the user stare at an empty screen you can provide a static image. Once the application is loaded this image will be replaced by the actual UI. You can set up this image by using the splashScreenImage attribute in the main application file:
<?xml version="1.0" encoding="utf-8"?> <s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.ASimpleMobileAppHome" sessionCachingEnabled="true" splashScreenImage="@Embed(source='loading.png')"> ... </s:MobileApplication>
Where to go next
The Flex “Hero” release will make the mobile development much easier – even with the current preview release one can enjoy productivity boosts. Understanding the screen metaphor and how it is implemented in the Flex framework is an important first step, especially for people coming from the world of web/desktop development.
If you want to try for yourself the best thing to do is to go to Adobe Labs, grab the Flash Builder “Burrito” bits and read Ryan’s post on how to set up your environment to support PlayBook development.
Comments
46 Responses to “Understanding Flex Mobile View and ViewNavigator”
Leave a Reply






[...] This post was mentioned on Twitter by Peter Elst and others. Peter Elst said: RT @mcorlan: Just blogged Understanding Flex Mobile Views and ViewNavigator: http://bit.ly/edZ7hu [...]
Mate, this is really good article.
I have one question regarding View lifecycle. If I’m using pushView to setup a new view, what happens with previous view. Is it destroyed because of FlexEvent.VIEW_DEACTIVATE event?
Cheers,
Ivan
@Ivan Ilijasic
Yes, this is the default behavior. If you don’t want to be destroyed you have to setup the destructionPolicy attribute to none for that View.
How much of this would be the same for iOS dev? Like the syntax in the application descriptor file.
Thanks
Thank you, mr. evangelist for your quick reply :) I tested event dispatching but I wanted to be sure that I tested right events.
Cheers,
Ivan
@Jessie
Flex Hero is coming to iOS this year. Other than this I can’t comment on the app descriptor file differences. Just wait a little bit and you will see :)
:) Just thought I would ask…
Thank you for the reply.
Jessie
Thanks for a great post Mihai. I’ve been working with Burrito for a month now and really like the mobile components.
Also, the Max Companion App is very nice, but, just curious how they did the Schedule part…since I didn’t get to go to Max (sigh!), is there a demo login?
Thanks again…look forward to more mobile postings on Burrito!!
@David Welch
I think there is no demo login. Sorry for this :D Be ready this year.
Thanks for the comment!
PS. More blog posts will come, keep en eye on my blog
[...] in mind. A base Flex Mobile application gives you an ActionBar which can contain global content, an easy way to add and remove pages from within the application (complete with default transitions between screens) and components for [...]
[...] dev who doesn’t want to drop down to only AS3. The other one is by Mihai and covers using the View and ViewNavigator classes in Flex “Hero”. While not PlayBook specific, they’re incredibly helpful when it comes to building mobile [...]
[...] dev who doesn’t want to drop down to only AS3. The other one is by Mihai and covers using the View and ViewNavigator classes in Flex “Hero”. While not PlayBook specific, they’re incredibly helpful when it comes to building mobile [...]
[...] Mihia Corlan – Understanding Flex Mobile View and ViewNavigator [...]
[...] mind. A base Flex Mobile application gives you an ActionBar, which can contain global content, an easy way to add and remove pages from within the application (complete with default transitions between screens), and components for [...]
[...] can use Flash Builder to create mobile views for adding the media assets. Learn more on Flex Mobile Views and Navigators. You can drag and drop images on the stage using the design [...]
[...] Ok, today I’m at the 360Flex conference following Brent Arnold’s AIR for Android presentation. We are playing with the Flex SDK 4.5 to build an Android app however we had to sign an NDA, so I cannot say some of the detail unless it’s already on the internet. This said I assume that tomorrow Adobe will announce the official release of Flash Builder 4.5. And a ton’s of information was already published: http://labs.adobe.com/technologies/flex/mobile/http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Herohttp://corlan.org/2011/01/12/understanding-flex-mobile-views-and-viewnavigator/ Here is TourDeFlex that shows all the capabilities of the Adobe AIR runtime. Download TourDeFlex. The source code can be found at https://github.com/jamesward/TourDeMobileFlex In the training we created a small application that checks the capabilities of your device: Download DeviceCapabiliites The application uses the Capabilities api that checks various parameters of your device., such as multi-touch, camera, location. Here is the source code: DeviceCapabilities.mxml <?xml version=“1.0” encoding=“utf-8”?><s:ViewNavigatorApplication xmlns:fx=“”http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" firstView=“views.DeviceCapabilitiesHomeView”]]]]><fx:Declarations><!— Place non-visual elements (e.g., services, value objects) here —></fx:Declarations></s:ViewNavigatorApplication> DeviceCapabilitiesHomeView.mxml<?xml version=“1.0” encoding=“utf-8”?><s:View xmlns:fx=“”http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title=“Device Capabilities”creationComplete=“view1_creationCompleteHandler(event)”]]]]><fx:Script><![CDATA[import mx.events.FlexEvent;import flash.sensors.Accelerometer; import flash.sensors.Geolocation; protectedfunction view1_creationCompleteHandler(event:FlexEvent):void{ var deviceString:String = "hasAudio: “ + Capabilities.hasAudio + ”s9">"n" + "hasMP3: “ + Capabilities.hasMP3 + ”s9">"n" + "language: “ + Capabilities.language + ”s9">"n" + "manufacturer: “ + Capabilities.manufacturer + ”s9">"n" + "os: “ + Capabilities.os + ”s9">"n" + "screenDPI: “ + Capabilities.screenDPI + ”s9">"n" + "screenResolutionX: “ + Capabilities.screenResolutionX + ”s9">"n" + "screenResolutionY: “ + Capabilities.screenResolutionY + ”s9">"n" + "touchscreenType: “ + Capabilities.touchscreenType + ”s9">"n" + "version: " + Capabilities.version; device.text = deviceString; accel.text = String(Accelerometer.isSupported); camera.text = String(Camera.isSupported); geo.text = String(Geolocation.isSupported); var multitouchString:String = "maxTouchPoints: “ + Multitouch.maxTouchPoints + ”s9">"n" + "supportedGestures: “ + Multitouch.supportedGestures + ”s9">"n" + "supportsGestureEvents: “ + Multitouch.supportsGestureEvents + ”s9">"n" + "supportsTouchEvents: " + Multitouch.supportsTouchEvents; multitouch.text = multitouchString; } ]]></fx:Script><s:Scroller width=“”s9">100%" height=“”s9">100%“”s3">><s:Group> <s:Label x=“10” y=“10” text=“Device Capabilities”/> <s:TextArea id=“”s9">device" left=“”s9">2" right=“”s9">2" top=“”s9">35" height=“”s9">231“”s3">/> <s:Label x=“”s9">10" y=“”s9">286" text=“”s9">Accelerometer“”s3">/> <s:Label x=“”s9">10" y=“”s9">317" text=“”s9">Camera“”s3">/> <s:Label x=“”s9">10" y=“”s9">348" text=“”s9">Geo Location“”s3">/> <s:Label x=“10” y=“379” text=“Mutlitouch Support:”/> <s:TextArea id=“”s9">accel" x=“”s9">190" y=“”s9">276" width=“”s9">280" height=“”s9">32“”s3">/> <s:TextArea id=“”s9">camera" x=“”s9">190" y=“”s9">308" width=“”s9">280" height=“”s9">32“”s3">/> <s:TextArea id=“”s9">geo" x=“”s9">190" y=“”s9">339" width=“”s9">280" height=“”s9">32“”s3">/> <s:TextArea id=“”s9">multitouch" left=“”s9">2" right=“”s9">2" top=“”s9">402" height=“”s9">173“”s3">/></s:Group></s:Scroller></s:View> Within Flash Builder in the properties of your project you can go to the Flex Build Packaging | Google Android and create a self-signed certificate.Then you can use the Project | Export Release Build… option to create an .apk file for your project. The application itself is not that exicting but allowed us to discover how to create AIR apps for Android using Flex and Flash Builder. Enjoy! Daniel. [...]
[...] June). This is great because Flex makes mobile development much easier – its support for screen metaphor, application session caching its a huge productivity [...]
[...] June). This is great because Flex makes mobile development much easier – its support for screen metaphor and application session caching is a huge productivity [...]
I have 2 doubts,
1. How can I remove one view from the stack? so that when user clicks the hardware back button, it will take to a previous view instead of the immediate previous view. For example, I have 3 views, view1, view2 and view3. I started with view1, moved to view2 and then view3. Now when the user clicks back, how can i skip the view2 and load view1 instead? In Java android, we can skip an activity from adding to the stack by calling “finish”.
2. Is it possible to have a view as an overlay? What I am looking for is to have a view2 (popup style) overlay the view1. And the hardware back button should remove the view2 and show view1.
Thanks in advance.
I’m using a ViewNavigator with the destructionPolicy=”none” attribute set in the View. However, when I navigate away from the view and then return, the whole view is loaded from scratch, which in this case is slow.
Am I missing something? Is there a trick to getting the View + it’s state to be cached?
Thanks!
@TomC
What Flash Builder and Flex framework build are using?
I’m not using any ViewNavigator in my application. Is it possible to move to the previous screen or the next screen using some property like popView or pushView etc?
Thanks!!
@Preetam
If you don’t use Flex mobile framework then you don’t have support for screen management. You have to implement your own screen management.
Mihai
Thanks 4 the reply. My case is: “In a Flex Mobile application,I have to write actionBarVisible = false; on creationComplete and on the click of an arrow button (which is at bottom of a Panel),I have to move to the previous screen. Currently,when I am writing navigator.popView() on clicking of that button,it’s give me error saying “Null Object Refference”.
[...] Im Prinzip ist der ViewNavigator ein Array, mit Array-typischen Methoden wie push() und pop(). Darin sind alle Views enthalten, jeder View hält eine Referenz auf den ViewNavigator. Das Prinzip ist nicht Thema dieses Posts, gut erklärt ist es hier. [...]
Thank you! Just Great …
Hi,
I’ve been hearing more and more that MXML is not encouraged for Flex Mobile. Is this really the case? I understand for skins, but for views too? If so, I’ll make my views in AS, but I can’t seem to figure out how to add and listen for the back button in the ActionBar.
Thanks for any input.
Hi, I have a TabbedViewNavigatorApplication with 3 views(ViewNavigator),
I can use the navigator.pushView(view,dataobject) from actionscript to navigate to another view with a data object of my choice.
How do I pass a data object of my choice when the user clicks on the default tab button along the bottom to switch views?
@bmanderscheid
You can use MXML for creating the Views. It is the same thing as using ActionScript (Flex compiler compiles all the MXML files to ActionScript classes).
Using MXML graphics for creating skins can be something that hearts performance especially for lists. So this is where ActionScript is better or FXG/bitmaps.
Mihai Corlan
Thanks, that really clears up a lot! I figured it did that so it was strange when I was told no MXML at all is best. When you say MXML graphics, that the same as new MXML Skin, correct?
I mean using MXML for drawing/creating skins like you’d normally do for Flex 4 components on a desktop (rectangle, lines, and so on).
Thanks. It’d be nice if Catalyst would spit out some AS Skins.
Great post, helped me understand some thing that I was missing.
one thing this does not work “sessionCachingEnabled=”true
Not sure why
I’m working on my first Flex Mobile application after spending most of my time in AS3 projects. I’m trying to figure out how the MAX companion app keeps the bottom bar while presenting new views. Is there a look under the hood of the MAX companion that I could use for reference?
@Almog
sessionCachingEnabled was the name of the attribute during the public beta. For the final release it was changed to persistNavigatorState with the possible values false / true.
@Mark
The source code of that application is not public.
Hello, thanks for this great post, I have a question about passing variables from one view to another, and it is driving me crazy since yesterday, any input is highly appreciated…
One of the View Files has an image with an ID
I need to assign a source to it from another view, and I tried every which way, like this
//second view function
private function pictureClickHandler (event:MouseEvent):void
{
navigator.pushView(TestingTab,{imageID:”images\Street_Pictures01.jpg”});
}
I also tried a bunch of other techniques to no avail.
Is there any way we can remove the default pushView() transition. as per my requirment i need to show a login page while while changing the tab. What i am doing is pushing the login view on selected tab’s wiew navigator object which shows the default transition. i need to show the login view directly without animating.
Please help.
[...] FlexMobile Views And ViewNavigator [...]
How can I show the Busy component while the view is loading and hide it when it is complete?
Thanks
Great summary mate.
Hi, i want to implement view navigator logic on a popup. I have a button called settings and clicking on that I’m creating a popup with different items in it. Clicking on each item inside the popup the present view should be pushed and clicked view should come up.
Im using this is in Qnx container.Can any one please help me how can we implement push and pop with out using view navigator application
Thanks.
I have recently developed an application on flex for ipad, in which i have an array updated and the array is accessed by an actionscript to take particular actions, But after a couple days that which i installed the app on ipad; the app dosenot seems to get proper access to the actionscript, Could you anyone tell me why? Please help…..
Flex Development on iOS & Andriod…
lash Builder 4.5, we can build mobile Flex applications for the Google Android platform. We can also build mobile ActionScript applications for both Android and Apple iOS. In June 2011, shortly after the release of Flex and Flash Builder 4.5,……
@Manu George: You asked, “How can I remove one view from the stack?”
I was looking for the same thing and realized that there is a replaceView() method that allowed me to in effect skip a view in the stack.
I wanted to go from Start > Play > Score and then have the previous button go from Score > Start. So when I go from Play > Score in the Play view I use replaceView(Score) rather than pushView(Score). This removes Play from the stack and the popView() in Score takes me to Start.
Ugghhhh… Not having any luck at all with destructionPolicy. I have a view that plays streaming video. When I popToFirstView the video keeps playing. It shouldn’t. Default behavior is to destroy the view. If I then come back to that view, another copy of the stream starts. As many times as I navigate away and then come back, a new stream starts up – eventually so many that the app dies. Either I don’t get destructionPolicy or that’s a really bad bug. When I post this in various places I hear crickets.