I want to pass data back to the prior view when the popView() or popToFirstView() method is called.
Passing data on pushView() is easy since it takes a data property. However, popView and popToFirstView() do not, so it isn't quite so obvious. Nonetheless, once you know the solution, this turns out to be a very simple matter of overriding a method in the View being popped.
First let's look at how you pass the data from the View being popped. In the below code, you'll notice that I have a button that triggers popView() and another that triggers popToFirstView() (note: popView() is also triggered if you use the device's built in back button). In order to pass data back I am overriding a method called createReturnObject() in the View class. Both the popView() or the popToFirstView() calls will trigger this method.
<?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="PassingDataPopPage2">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
override public function createReturnObject():Object {
var returnedObject:Object = new Object();
returnedObject.myValue = dataTxt.text;
return returnedObject;
}
protected function button1_clickHandler(event:MouseEvent):void
{
navigator.popView();
}
protected function button2_clickHandler(event:MouseEvent):void
{
navigator.popToFirstView();
}
]]>
</fx:Script>
<s:TextInput id="dataTxt" x="10" y="10" width="460" text="I am data."/>
<s:Button x="10" y="73" width="460" label="Pop Me or Click Back Button" click="button1_clickHandler(event)"/>
<s:Button x="10" y="156" width="460" label="Pop to First View" click="button2_clickHandler(event)"/>
</s:View>
protected function view1_viewActivateHandler(event:FlexEvent):void
{
if (returnedObject != null) {
dataTxt.text = returnedObject.myValue;
}
}
+