Solved

Flex Mobile - check current view

Posted on 2013-05-19
2
889 Views
Last Modified: 2013-05-23
Hello

I am having a problem, with something I don't understand why:

I have a timer that will start(timer will still run when you are on other view), I wan't the timer handler to only do something when I am on the current view:

if (navigator.activeView.className == 'TestView'){
                                          //function
}

It gives an null error when I change the view and the timer handler fires:
Cannot access a property or method of a null object reference.

Yes, I have a trace function in the init function on the other views that I change to and it gives a valid name back in the 'console', why does my if function give an null error when it is not == with the current view class name, I also now made a trace before the if statement, it seems that the timers current view is null on other views but not on the view that the timer started on?

Here is a demo project showing you the problem:
http://www.megafileupload.com/en/file/418802/TestCurr-fxp.html
0
Comment
Question by:JoachimPetersen
  • 2
2 Comments
 
LVL 19

Expert Comment

by:Gary Benade
ID: 39180173
Try wrapping the call in a callLater to delay it by 1 frame and give the navigator time to initailize:

callLater( function()
{
    if (navigator.activeView.className == 'TestView')
    {
          //function
    }
});
0
 
LVL 19

Accepted Solution

by:
Gary Benade earned 500 total points
ID: 39180713
I'm not sure why you need the timer? Your timer was repeating, you named the timer function runOnce so I assume you didn't want that behaviour - I have corrected this in my code.

Without a timer doing this will work:
			protected function int():void
			{
				if (navigator.activeView.className == 'TestCurrHomeView'){
					trace('working'); //a functionm here if the curr view is right
				}
			}

Open in new window


If you really need the timer..
			protected function int():void{
				var myTimer:Timer = new Timer(1000,1); // 1 second, run once
				myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, runOnce);
				myTimer.start();
			}
			
			private function runOnce(event:TimerEvent):void {
				trace(navigator.activeView.className);
				if( navigator){ // a non-active view wont have a navigator
					if (navigator.activeView.className == 'TestCurrHomeView'){
						trace('working'); //a function here if the curr view is right
					}
				}
			}

Open in new window


You could also listen for the Navigator objects change event and base your logic on that:
			import spark.events.ElementExistenceEvent;

			protected function int():void{
				navigator.addEventListener(ElementExistenceEvent.ELEMENT_ADD, tabChanged);
			}
			
			private function tabChanged( e:ElementExistenceEvent):void{
				trace( navigator.activeView );
				if (navigator.activeView == this){
					trace('working'); //a function here if the curr view is right
				}
			}

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now