Avg. Rating 1.0
Tags:



Problem

I want to pass data back to the prior view when the popView() or popToFirstView() method is called.

Solution

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.

Detailed explanation

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>
    
 
In order to receive the data in the prior View, you will simply look for returnedObject within your viewActivate handler method as below:
 
                protected function view1_viewActivateHandler(event:FlexEvent):void
{
   if (returnedObject != null) {
      dataTxt.text = returnedObject.myValue;
   }
}
    
 
This recipe is excerpted from my blog post at  http://remotesynthesis.com/post.cfm/passing-data-on-pop-methods-in-mobile-flex. Feel free to follow  my blog for more Flex and ActionScript tips, inluding mobile.

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes