Solved

Flex Mobile - check current view

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Flex 3 and Flex 4 Wasi Dashboard modify graphs in Pod.mxl datasource 3 440
Automatic Notifications 16 344
Sum column DataGrid 3 576
Flex AS3 image Compression 2 739
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…
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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