Solved

Flex Mobile - check current view

Posted on 2013-05-19
2
915 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 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