Search code examples
apache-flexflash-buildertextinputflex-spark

How to pass parameters to a view that was previously shown in Flex Mobile Project (I cannot use pushView)


I have a Flex Mobile Project that I am working on. I am trying to pass parameters from one view to another. The only problem is that I cannot use navigator.pushView to push the View and the parameter as the view I am pushing to was the previous view. So this wipes out using the addHandler() and the returnObjectsCreated() as I cannot use pushView. I am having to use popView because it is my previous view that I have to pass parameters too. Any help would be appreciated.

That class that has the parameters I need to pass is below. It is a view that shows a list. So I need to pass list.selectedItem to the popview or previous view...

<?xml version="1.0" encoding="utf-8"?>
<amec:BaseBrowseView xmlns:fx="http://ns.adobe.com/mxml/2009" 
              xmlns:s="library://ns.adobe.com/flex/spark" 
              xmlns:amec="com.amec.Components.*"
              title="Select an item">

<fx:Script>
    <![CDATA[
        import com.amec.BaseSql;

        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        import spark.events.IndexChangeEvent;

        [Bindable]private var resultArr:ArrayCollection = new ArrayCollection();

        protected function myList_changeHandler(event:IndexChangeEvent):void 
        {

            navigator.popView();


            //Either send a ref to the last view or override createReturn

        }


        [Bindable(event="myDataChanged")]
        private function get myData():ArrayCollection
        {

                           }



    ]]>
</fx:Script>

<s:List id="list"         
        height="100%" width="100%" 
        dataProvider="{myData}" 
        labelField="DMV_VALUE_1"
        change="myList_changeHandler(event);">
</s:List>


<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

Now below is the previous view that I want to popView to that I need to pass parameters to so I can populate the TextInput with.

<?xml version="1.0" encoding="utf-8"?>
<amec:BaseControl xmlns:fx="http://ns.adobe.com/mxml/2009" 
              xmlns:s="library://ns.adobe.com/flex/spark" 
              xmlns:amec="com.amec.Components.*"
              horizontalCenter="true">

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        [Bindable]
        protected var textValue:String;

        protected function control_creationCompleteHandler(event:FlexEvent):void
        {
            // todo: get control data from view.data


        }

        protected function control_clickHandler(event:MouseEvent):void
        {

            parentView.navigator.pushView(TextListView);

        }




    ]]>
</fx:Script>

<s:Label text="Robert says something meaningful goes here" />

<s:TextInput id="ns" text="{textValue}" editable="false" click="control_clickHandler(event)"/>





<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

Again I cannot use pushView as the view is already on the stack.


Solution

  • I looked at using static variables but that was a little messy, I also looked at using Dependency Injection but spending a few hours to set up the framework and get it running is to costly when all I am doing is passing parameters on a popView, so what I came up with is the similar approach I have used when doing Native Android Apps with the Android SDK. A Singleton with getters and setters. Basically the way I have been able to do this is with the use of a Singleton sharedInstance. See code below:

    package com.controls.Text
    {
        import flash.utils.Dictionary;
        [Bindable]
    
        public class ParameterManager
        {
            private static var instance:ParameterManager = new ParameterManager();
            private var dictionary:Dictionary=new Dictionary(); 
    
    
            public function ParameterManager( )
            {
            if (instance != null) { throw new Error('Must use ParameterManager.getInstance().') }
            }
    
            public static function getInstance(): ParameterManager {
                return instance;
            }
    
            public function setParameter(key:*, value:*):void
            {
            dictionary[key]=value;  
            }
    
            public function getParameter(key:*):*
            {
                return dictionary[key];
                }
        }
    }
    

    The method setting the value of the parameter:

            protected function myList_changeHandler(event:IndexChangeEvent):void 
            {
                var listViewReturnObject:String = new String();
                listViewReturnObject = list.selectedItem.DMV_VALUE_1;
                ParameterManager.getInstance().setParameter("selectedItem", listViewReturnObject);
    
                navigator.popView();
            }
    

    The method getting the value:

            protected function control_creationCompleteHandler(event:FlexEvent):void
            {               
                var listViewReturnObject:Object = new Object();
    
                listViewReturnObject= ParameterManager.getInstance().getParameter("selectedItem");
                if (listViewReturnObject != null)
                {
                    dataTxt.text= String(listViewReturnObject);
                    ParameterManager.getInstance().setParameter("", null); //Make sure objects are cleared when done.
                }
            }