ViewNavigatorEvent.REMOVING
constant defines the value of the
* type
property of the event object for an removing
* event. This event is dispatched when a screen is about to be replaced by
* another screen through a navigator action. If preventDefault()
* is called on this event, the view removal will be canceled.
*/
public static const REMOVING:String = "removing";
/**
* The ViewNavigatorEvent.VIEW_ACTIVATE
constant defines the value of the
* type
property of the event object for an viewActivate
* event. This event is dispatched when a component is activated.
*/
public static const VIEW_ACTIVATE:String = "viewActivate";
/**
* The ViewNavigatorEvent.VIEW_DEACTIVATE
constant defines the value of the
* type
property of the event object for an viewDeactivate
* event. This event is dispatched when a component is deactivated.
*/
public static const VIEW_DEACTIVATE:String = "viewDeactivate";
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
/**
* Constructor.
*
* @param type The event type; indicates the action that caused the event.
*
* @param bubbles Specifies whether the event can bubble up
* the display list hierarchy.
*
* @param cancelable Specifies whether the behavior
* associated with the event can be prevented.
*
* @param action The navigation action committed by the view navigator
* that resulted in the event.
*/
public function ViewNavigatorEvent(type:String, bubbles:Boolean = false,
cancelable:Boolean = false,
action:String = null);
//----------------------------------
// action
//----------------------------------
/**
* The navigation action committed by the view navigator that resulted
* in the event.
* Possible actions include a view being activated or deactivated.
*/
public var action:String;
}
}
### View
View is the base class for all views. Developers should use this as their base class for their custom views.
package spark.components
{
//--------------------------------------
// SkinStates
//--------------------------------------
/**
* The skin state used when the view is in portrait orientation
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
SkinState("portrait")
/**
* The skin state used when the view is in landscape orientation
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
SkinState("landscape")
//--------------------------------------
// Events
//--------------------------------------
/**
* Dispatched when the back key is pressed when a view exists inside
* a mobile application.
*/
Event(name="backKeyPressed", type="mx.events.FlexEvent")
/**
* Dispatched when the data
property changes.
*/
Event(name="dataChange", type="mx.events.FlexEvent")
/**
* Dispatched when the menu key is pressed when a view exists inside
* a mobile application.
*/
Event(name="menuKeyPressed", type="mx.events.FlexEvent")
/**
* Dispatched when the current view has been activated.
*/
Event(name="viewActivate", type="spark.events.ViewNavigatorEvent")
/**
* Dispatched when the current view has been deactivated.
*/
Event(name="viewDeactivate", type="spark.events.ViewNavigatorEvent")
/**
* Dispatched when the screen is about to be removed in response
* to a screen change. Calling preventDefault()
* while handling this event will cancel the screen change.
*/
Event(name="removing", type="spark.events.ViewNavigatorEvent")
class View extends SkinnableContainer implements IDataRenderer
{
//-----------------------------------------------------------
//
// Properties
//
//-----------------------------------------------------------
/**
* Flag indicating whether the current view is active.
*
* @default false
*/
public function get isActive():Boolean;
/**
* The string that describes the context in which this view was
* created. This property is assigned to the context
* parameter that is passed into ViewNavigator.pushView()
.
*
* @default null
*
* @see spark.components.ViewNavigator
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public function get context():String
/**
* The data object for the view.
*
* @default null
*/
public function get data():Object;
public function set data(value:Object):void;
/**
* Stores the destruction policy of the view. If set to
* "auto", the view is automatically destroyed as new views
* are added. If set to "none", the view instance remains
* in memory.
*
* @default "auto"
*/
Inspectable(category="General", enumeration="auto,never", defaultValue="auto")
public function get destructionPolicy():String;
public function set destructionPolicy(value:String):void;
/**
* A reference to the ViewNavigator the view lives in.
*
* @default null
*/
public function get navigator():ViewNavigator;
/**
* Determines the way the navigator's control bars should be
* laid out. If true, the controls hover on top of the current
* view. If false, the components will follow a standard
* vertical layout.
*
* @default false
*/
public function get overlayControls():Boolean;
public function set overlayControls(value:Boolean):void
//----------------------------------------------------------
//
// Template Properties
//
//----------------------------------------------------------
/**
* Flag indicating whether a view should show the action bar or not.
*
* @default true
*/
public function get actionBarVisible():Boolean;
public function set actionBarVisible(value:Boolean):void;
/**
* An array of VisualElements that the current view would like
* displayed in the ActionBar's action content area.
*
* @default null
*/
public function get actionContent():Array;
public function set actionContent(value:Array):void;
/**
* The layout the view would like assigned to the ActionBar's
* action content group.
*
* @default HorizontalLayout
*/
public function get actionLayout():LayoutBase;
public function set actionLayout(value:LayoutBase):void;
/**
* An array of VisualElements that the current view would like
* displayed to the ActionBar's navigation content area.
*
* @default null
*/
public function get navigationContent():Array;
public function set navigationContent(value:Array):void;
/**
* The layout the view would like assigned to the ActionBar's
* navigation content group.
*
* @default HorizontalLayout
*/
public function get navigationLayout():LayoutBase;
public function set navigationLayout(value:LayoutBase):void;
/**
* The title of the view. This string is displayed in the
* ActionBar's content group area if the titleContent property
* isn't set.
*
* @default
*/
public function get title():String;
public function set title(value:String):void;
/**
* An array of VisualElements that the current view would like
* added to the ActionBar's title content area. If this is
* null, the ActionBar will display the title of the view.
*
* @default null
*/
public function get titleContent():Array;
public function set titleContent(value:Array):void;
/**
* The layout the view would like assigned to the ActionBar's
* title content group.
*
* @default HorizontalLayout
*/
public function get titleLayout():LayoutBase;
public function set titleLayout(value:LayoutBase):void;
//-----------------------------------------------------------
//
// Methods
//
//-----------------------------------------------------------
/**
* Methods creates an object that should be returned to the
* previous screen when this view is popped off a navigator's
* stack.
*
* @return null
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public function createReturnObject():Object;
/**
* Returns the name of the state to be applied to the view.
* By default, this method will return "portrait" if a portrait
* state is defined and the stage orientation is also portrait.
* "landscape" is returned if the view has defined a landscape
* state and the device is also in a landscape orientation.
* Otherwise null is returned.
*
* @param isLandscape Indicates whether that application is
* currently in a landscape orientation.
*
* @return null
*/
public function getCurrentViewState(isLandscape:Boolean):String;
}
}
### [ContainerDestructionPolicy](ContainerDestructionPolicy)
This class is used to set the destructionPolicy of the View and ViewNavigator classes. When used on a View, it determines whether the view instance should be deleted when another view is pushed on top of it.
package spark.core
{
/**
* The ContainerDestructionPolicy class defines the constant values
* for the destructionPolicy
property of spark view
* classes.
*/
public final class ContainerDestructionPolicy
{
//--------------------------------------------------------------------------
//
// Class constants
//
//--------------------------------------------------------------------------
/**
* The lifespan of the container's children will automatically
* be managed by the container based on the container's own
* heuristic. The default implementation mimics that behavior of
* ContainerDestructionPolicy.ALWAYS
*/
public static const AUTO:String = "auto";
/**
* The container will never destroy its children.
*/
public static const NEVER:String = "never";
}
}
### [ViewNavigatorBase](ViewNavigatorBase)
This is the base class for all ViewNavigator.
package spark.components.supportClasses
{
//--------------------------------------
// Events
//--------------------------------------
/**
* The ViewNavigatorBase class defines the base class logic and
* interface used by view navigators. This class contains
* methods and properties related to view management, as well
* as integration points with ViewNavigatorApplicationBase application
* classes.
*/
public class ViewNavigatorBase extends SkinnableContainer
{
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* Flag indicating whether the navigator is active. The parent navigator
* will automatically set this flag to true or false as its state changes.
*
* @default true
*/
public function get isActive():Boolean;
/**
* The currently active view of the navigator. Only one view can
* be active at a time.
*
* @default null
*/
public function get activeView():View;
/**
* Returns the icon that should be used when this navigator is represented
* by a visual component.
*
* @default null
*/
public function get icon():Object;
public function set icon(value:Class):Object;
Bindable
/**
* The label to be used when this stack is represented by a visual component.
*
* @default null
*/
public function get label():String;
public function set label(value:String):void;
/**
* The last action performed by the navigator.
*
* @default ViewNavigatorAction.NONE
*/
public function get lastAction():String;
/**
* Flag indicates how the navigator's ui controls should be
* laid out in relation to the active view. If true, the view
* will extend the entire content area of the component, and the
* ui controls will hover on top.
*
* Changing this property will result in a skin validation.
*
* @default true
*/
public function get overlayControls():Boolean;
public function set overlayControls(value:Boolean):void;
/**
* The parent navigator for this navigator. This is only used
* if a navigator exists as a child of another navigator.
*
* @default null
*/
public function get parentNavigator():ViewNavigatorBase;
/**
* Flag indicating whether transitions are played by the
* navigator when a view changes or when the actionBar or tab bar
* visibility changes.
*
* @default true
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public function get transitionsEnabled():Boolean;
public function set transitionsEnabled(value:Boolean):void;
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/**
* This method updates various properties of the navigator when a
* new view is added and activated.
*
* @param view The view that was added
*/
public function updateControlsForView(view:View):void;
}
}
### [ViewNavigatorAction](ViewNavigatorAction)
package spark.components.supportClasses
{
/**
* The ViewNavigatorAction class defines the constant values
* for the lastAction
property of ViewNavigator.
* It indicates what the last committed operation was.
*/
public final class ViewNavigatorAction
{
//--------------------------------------------------------------------------
//
// Class constants
//
//--------------------------------------------------------------------------
/**
*
*/
public static const NONE:String = "none";
/**
*
*/
public static const PUSH:String = "push";
/**
*
*/
public static const POP:String = "pop";
/**
*
*/
public static const POP_ALL:String = "popAll";
/**
*
*/
public static const POP_TO_FIRST:String = "popToFirst";
/**
*
*/
public static const REPLACE_VIEW:String = "replaceView";
/**
*
*/
public static const REPLACE_STACK:String = "replaceStack";
}
}
### ViewNavigator
This is the api exposed by ViewNavigator.
package spark.components
{
DefaultProperty("navigationStack")
//--------------------------------------
// SkinStates
//--------------------------------------
/**
* The state used when the navigator is in portrait orientation
*/
SkinState("portrait")
/**
* The state used when the navigator is in landscape orientation
*/
SkinState("landscape")
/**
* The state used when the navigator is in portrait orientation
* and the navigator controls are overlaid on top.
*/
SkinState("portraitAndOverlay")
/**
* The state used when the navigator is in landscape orientation
* and the navigator controls are overlaid on top.
*/
SkinState("landscapeAndOverlay")
/**
* The ViewNavigator component is a container that consists of a collection of
* View objects, where only the top most view is visible and active. Views can
* be added and removed by using the popView() and pushView() methods on the
* navigator.
*
* When a new view becomes active, the old view's instance is destroyed.
* When a view is pushed on top of navigator's stack, the old view's data
* property is automatically persisted and restored when it is reactived
* through one of the pop methods.
*
* ViewNavigator doesn't provide index information about the Views that
* it is currently managing and strictly follows a stack navigation model.
* Jumping between a set of views similar to a ViewStack is not supported
* by this component.
*
* ViewNavigator displays an optional action bar that displays contextual
* information defined by the active view. When the active view changes, the
* action bar is automatically updated.
*/
public class ViewNavigator extends ViewNavigatorBase
{
//--------------------------------------------------------------------------
//
// Skin Parts
//
//--------------------------------------------------------------------------
/**
* A skin part that defines the action bar of the navigator.
*/
SkinPart(required="false")
public var actionBar:ActionBar;
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* The string that describes the context in which the current view was
* created. This property is assigned to the context
* parameter that is passed into ViewNavigator.pushView()
.
*
* @default null
*
* @see spark.components.ViewNavigator
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public function get context():Object;
/**
* This property is the object to use to initialize the first view
* of the stack. This must be a class that extends View
.
*
* @default null
*/
public function get firstView():Class;
public function set firstView(value:Class):void;
/**
* This is the initialization data to pass to the first view when the
* navigator is initialized.
*
* @default null
*/
public function get firstViewData():Object;
public function set firstViewData(value:Object):void;
/**
* Returns the number of views being managed by the navigator.
*/
public function get length():int;
/**
* Holds the return object returned by the last view that was popped
* off the navigation stack. This object is only available when the
* navigator is in the process of switching views in response to a
* pop navigation operation.
*
* This object is guarenteed to be valid when the a view receives
* the FlexEvent.ADD
event, and is destroyed after
* the view receives a ViewNavigatorEvent.VIEW_ACTIVATE
event.
ViewNavigator.pushView()
method.
*
* @see spark.components.View
* @see spark.components.ViewNavigator#pushView
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public class ViewReturnObject
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
/**
* Constructor.
*
* @param object The returned object
* @param context The context in which the owner was created
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public function ViewReturnObject(object:Object, context:String);
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
//----------------------------------
// object
//----------------------------------
/**
* The return object generated by the view that is being removed.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var object:Object = null;
//----------------------------------
// context
//----------------------------------
/**
* The context identifier passed to the popped view when it was pushed
* onto the navigation stack.
*
* @see spark.components.ViewNavigator#pushView
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var context:String = null;
}
}
**The following classes are private to the implementation details of** **ViewNavigator** **and related features .**
### [NavigationStack](NavigationStack)
This is a data structure that manages the stack of views used by a navigator.
package spark.components.supportClasses
{
import flash.utils.IExternalizable;
/**
* The NavigationStack class is a data structure that is internally used by
* ViewNavigator to track the current set of views that are being managed
* by the navigator.
*/
public class NavigationStack implements IExternalizable
{
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* Returns the length of the stack.
*/
public function get length():int;
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Clears the entire stack.
*/
public function clear():void;
/**
* Adds a view to the top of the navigation stack.
*
* @param factory The class of the View to create.
* @param data The data object to pass to the view when it is created
* @param context The context identifier to pass to the view when created
*
* @return The data structure that represents the current view.
*/
public function pushView(factory:Class, data:Object, context:String = null): ViewDescriptor;
/**
* Removes the top view off the stack.
*
* @return The data structure that represented the View.
*/
public function popView():ViewDescriptor;
/**
* Removes all but the root object from the navigation stack.
*
* @return The data structure that represented the View.
*/
public function popToFirstView(): ViewDescriptor;
}
}
### [ViewDescriptor](ViewDescriptor)
package spark.components.supportClasses
{
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.utils.IExternalizable;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import spark.components.View;
/**
* The ViewDescriptor object is a data structure used to store information
* about a view that is being managed by a ViewNavigator.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public class ViewDescriptor implements IExternalizable
{
/**
* Constructor.
*
* @param viewClass The class used to create the View
* @param data The data object to pass to the view when created
* @param context The context of the view
* @param instance A reference to the instance of the View
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public function ViewDescriptor(viewClass:Class = null,
data:Object = null,
context:Object = null,
instance:View = null);
/**
* The string that describes the context in which this view was
* created. This property is assigned to the context
* parameter that is passed into ViewNavigator.pushView()
.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var context:Object;
/**
* The current data object that is being used by the view. When a view
* is removed from a navigation stack, this value will be updated to
* match the view's instance's current data object.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var data:Object;
/**
* A reference to the instance that is represented by this view object.
* ViewNavigator will create and assign the instance as needed. This
* property will be nulled out when a view is destroyed.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var instance:View;
/**
* The serialized data that the view has requested be saved to disk when
* the application is writing data to a shared object or external file.
* This object is the result of the serializeData()
method
* on View.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var persistenceData:Object;
/**
* The class used to create the view. ViewNavigator will expect this
* class to subclass View.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/
public var viewClass:Class;
}
}
## Examples and Usage
### Basic Usage
The simple example below demonstrates how to set up a ViewNavigator inside an application and use its various features.
**a href="MyView">MyView***.mxml*
[MyView](MyView) is a View that contains three buttons that each execute a different navigation operation. The Pop and Pop to First View buttons are only shown if the navigator has more than one view in its navigation stack.
<s:view xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="View">
<s:layout>
<s:verticallayout>
</s:verticallayout></s:layout>
<s:button label="Push" click="navigator.pushView(MyView)">
<s:button label="Pop" click="navigator.popView()" visible="{navigator.length > 1}" includeinlayout="{navigator.length > 1}">
<s:button label="Pop to First View" click="navigator.popToFirstView()" visible="{navigator.length > 1}" includeinlayout="{navigator.length > 1}">
</s:button></s:button></s:button></s:view>
**a href="MyApplication">MyApplication***.mxml*
This is the main application file for the swf. It sets its initial view to [MyView](MyView), meaning that is the view that will
be instantiated when the app starts.
<s:application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<s:viewnavigator id="navigator" width="100%" height="100%" firstview="{MyView}">
</s:viewnavigator></s:application>
### Returning Data
Below is an example of two views using the context property to customize a view's appearance and return object.
**Favorites View**
This view allows the user to define what their favorite items are. When a category in the list is selected, the view will push on a view to ask the user what their favorite thing is for that category. When this view is recreated, it will display the user's favorite item at the bottom. This component uses the `context` parameter in `pushView` to tell `[QuestionView]` what question to ask. In addition, when instantiated, this view checks for a return object, and if there is one, displays that information at the bottom.
<s:view xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" add="view1_addHandler(event)" title="Favorites">
<s:layout>
<s:verticallayout gap="20">
</s:verticallayout></s:layout>
<fx:script>
</fx:script>
<s:list id="list" width="100%" height="100%" change="list1_changeHandler(event)">
<s:dataprovider>
<s:arraylist>
<fx:object label="Color" token="color">
<fx:object label="Food" token="food">
<fx:object label="Song" token="song">
<fx:object label="City" token="city">
<fx:object label="Animal" token="animal">
<fx:object label="Game" token="game">
</fx:object></fx:object></fx:object></fx:object></fx:object></fx:object></s:arraylist>
</s:dataprovider>
</s:list>
<s:group id="resultGroup" width="100%" minheight="40">
<s:label id="resultField" textalign="center" width="100%" bottom="10">
</s:label></s:group>
</s:view>
**Question View**
This view will change its prompt and title based on the context is was created in. When the view is removed, it returns the string in the text input component.
<s:view xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Favorite " add="view1_addHandler(event)">
<fx:script>
</fx:script>
<s:layout>
<s:verticallayout paddingleft="10" paddingright="10" paddingtop="10">
</s:verticallayout></s:layout>
<s:label id="labelField" horizontalcenter="0" width="100%">
<s:textinput id="responseField" width="100%" enter="navigator.popView()">
</s:textinput></s:label></s:view>
\\
## Additional Implementation Details
\\
## Compiler Work
\\
## Performance
* The main performance metric we will need to measure is the creation time of a view, as well as the time it takes to complete a full validation pass.
## Issues and Recommendations