Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Flex Mobile - check current view

Posted on 2013-05-19
2
Medium Priority
?
944 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

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…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

664 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