Solved

Flex Mobile - check current view

Posted on 2013-05-19
2
910 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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 previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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