2

Flex having any init(), destroy() method. Like Servlet init()method will run at Application initialize and never call it again if Refresh the Page also.

Raju
  • 176
  • 3
  • 11
  • Please accept the correct answers for the other questions that you have posted. Most of the questions you asked have valid answers ready to be accepted. – Chris Ghenea Jul 25 '11 at 16:14

2 Answers2

2

I would suggest not using initialize event, and instead use creationComplete. All UIComponent dispatch that event when they are finished constructing themselves AND their children. This event is executed once after the component has been initialized, had a chance to measure itself, perform layout, and added to the stage.

<mx:Application ... creationComplete="init()"/>
   <mx:Script>
       private function init() : void {
           ...  // put your initialization routine here
       }
   </mx:Script>
</mx:Application>
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • It's entirely contextual I guess. If you don't need to do anything with visual elements, such as making a service call, then using initialize is preferred. A large layout can take a long time to build where you may be wasting time that could be spent on requesting data from a server. – Jonathan Rowny Jul 25 '11 at 17:53
  • Yes, but when this person goes to try and pull something out of a component and gets null they'll be all perplexed why children are null or some property on the object hasn't been set. So to head off some of those potential issues use creationComplete. I'm glad you posted your perspective because it does show more depth to each thing we suggested. – chubbsondubs Jul 25 '11 at 18:18
1

All flex components, including the root "application" component have an "initlize" event that you can listen to and handle.

If you'd like it to only run ONCE, regardless of refresh, you'd need to store a variable somehow, such as with a local shared object. That's pretty easy to do:

private function onInit():void{
  var appSO:SharedObject = SharedObject.getLocal("yourappdata");
  if(appSO.size < 0){
    //do your init code
    appSO.data.initialized = true;
    appSO.flush();
  }
}
Jonathan Rowny
  • 7,588
  • 1
  • 18
  • 26