Mobile Flex: ViewNavigator Basics

Posted on May 15, 2011 at 8:45 am in Development

Flex 4.5 provides some pretty slick updates and enhancements, the least of are the of Mobile components and the ability to easily slam out some pretty nice mobile apps. The first thing I’d like to talk about is a new concept, the ViewNavigator. The ViewNavigator provides some pretty intense functionality such as view management.

What is the ViewNavigator?

The ViewNavigator keeps track of your views. It does this by keeping your views in a list.  To add a new view you ‘push’ the view into the list, to remove a view you can ‘pop’ a view out of the list. You can think of it as a stack – first in, last out – and the last view in is the visible view.

Popping a view out of the ViewNavigator's 'stack'

Pushing a View into ViewNavigator's 'stack'

Popping a view out of the ViewNavigator's 'stack'

Popping a view out of the ViewNavigator's 'stack'

Using the ViewNavigator

Using the view navigator is a pretty straight forward process of capturing a user interaction, such as a button click, then pushing the new View into the ViewNavigator’s stack.

For example, let’s pretend that you have a new Flex mobile project. The default view of that project has a button, that when clicked should display another view named MyNewView.  MyNewView also has a button, that when clicked returns you to the home view.

Home View Component

In the Home View component all you really need to worry about the click handler on the button:

label="NEXT"
width="100%"
click="navigator.pushView(MyNewView)" />

The click handler calls the pushView() method on ‘navigator‘, a property available from the View class, passing it the class name of the View that you want to display. We’ll cover getting data into that view and transitions in other posts.  The creation of the new View & default transition are all handled by the  framework.

MyNewView Component

The MyNewView View component is basically the same thing:

label="BACK"
width="100%"
click="navigator.popView()" />

You call popView() on the ‘navigator‘ property which removes the view from the stack displaying the Home view again.

Here is a quick screen cast of an application using similar code:
Get Adobe Flash player

* Required


Comments

  1. raja 24 October 2011 at 8:51 am permalink

    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.

    • John Crosby 24 October 2011 at 2:36 pm permalink

      The ViewNavigator has the push and pop view management baked in. If you don’t want to use the ViewNavigator, you’ll need to build your own system to manage the views using a push and pop view-stack API.

  2. newTween 15 November 2011 at 11:10 pm permalink

    Hi,
    thx for tutorial.
    there is typo in your code should be “navigator” instead of “navigtor”