UIComponent的生命周期(life cycle)

写flex组件,了解UIComponent的生命周期(life cycle)很重要,尤其是初始化(initialization)的过程很复杂,贴个文件可以很容易看清这个顺序

文件如下:

package
{
import flash.events.Event;

import mx.core.UIComponent;
import mx.events.FlexEvent;

public class UILifeCycle extends UIComponent
{
    public function UILifeCycle()
    {
   super();
     trace("***[ constructor ]");
     
     //flash.events.Event
     this.addEventListener(Event.ADDED,onAdded);
     this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
     this.addEventListener(Event.REMOVED,onRemoved);
     this.addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage);
    
     //mx.events.FlexEvent;
     this.addEventListener(FlexEvent.ADD,onAdd);
     this.addEventListener(FlexEvent.REMOVE,onRemove);
     this.addEventListener(FlexEvent.PREINITIALIZE,onPreinitialize);
     this.addEventListener(FlexEvent.INITIALIZE,onInitialize);
     this.addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete);
    
    }
   
    //flash.events.Event
    private function onAdded(eo:Event):void{trace(eo.toString())};
    private function onAddedToStage(eo:Event):void{trace(eo.toString())};
    private function onRemoved(eo:Event):void {trace(eo.toString())};
    private function onRemovedFromStage(eo:Event):void {trace(eo.toString())};
   
    //mx.events.FlexEvent;
    private function onAdd(eo:FlexEvent):void {trace(eo.toString())};
    private function onRemove(eo:FlexEvent):void{trace(eo.toString())};
    private function onPreinitialize(eo:FlexEvent):void{trace(eo.toString())};
    private function onInitialize(eo:FlexEvent):void{trace(eo.toString())};
private function onCreationComplete(eo:FlexEvent):void{trace(eo.toString())};

   override protected function createChildren():void{trace("***[ createChildren ]")}
   override protected function commitProperties():void{trace("***[ commitProperties ]")}
   override protected function measure():void{trace("***[ measure ]")}  
   override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{trace("***[ updateDisplayList]")}
  
}
}

拖到舞台上发布就可以看到trace信息了

***[ constructor ]
[Event type="added" bubbles=true cancelable=false eventPhase=2]
[Event type="add" bubbles=false cancelable=false eventPhase=2]
[Event type="preinitialize" bubbles=false cancelable=false eventPhase=2]
***[ createChildren ]
[Event type="initialize" bubbles=false cancelable=false eventPhase=2]
***[ commitProperties ]
***[ measure ]
***[ updateDisplayList]
[Event type="creationComplete" bubbles=false cancelable=false eventPhase=2]
[Event type="addedToStage" bubbles=false cancelable=false eventPhase=2]

你可能感兴趣的:(component)