QueTwo's Blog

thouoghts on telecommunications, programming, education and technology

Passing data back and forth to a View in a Mobile AIR application

On a recent AIR for Android app I was working on, I had the need to send data back from the current view to the my controller.  The app was really simple, but the ability to send back the data to my data controller changed it from a 4 hour app to an entire day project.  It turns out, when you design your app around the ViewNavigatorApplication or the TabbedViewNavigatorApplication, they make it really hard to get a reference to the instantiated View created by the navigator.push() function.  In fact, since the navigator.activeView casts everything as a View (and it does it on the next frame, so you can’t get that reference anyway), rather than your custom class, you can’t call any methods in that class, nor can you attach any custom events.

This makes it very difficult if you want your controller (or in my case, the main application) to push or pull any data to the View after it has been created.  I didn’t want to re-create the functionality of the ViewNavigatorApplication by hand — they did lots of really nice stuff in there, but the inflexibility seemed kind of odd.

Anyway, I remembered that you could bubble events from the view and have the main application be the event listener to catch it.  While it does have the added advantage of not directly creating a reference to the View (preventing it from being garbage collected), it feels really dirty in my mind.  I guess I’ll get over it.

Injecting data to the View from the controller is a bit more trickey.  What I ended up doing was creating a new class that extends EventDispatcher, and adding getters and setters that dispatch their own events.  I would then package that custom class in the data property and send it along in the push function.  I can then have that View listen to events off that class, as the class dispatches them with every change.  Again, this won’t cause any references to the View (which is what the SDK was trying to avoid), but I can still get my data there.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: