initialize() or creationComplete() method not invoked when revisiting the page in Flex
In my flex application I use the viewstack.selectedchild() property to change the views.
When I revisit the views by the viewstack.selectedchild() property the initialize method doesnot get invoked..
Any 开发者_Go百科help would be appreciated.
The object has already been created so it doesn't re-initialize it's already in memory (which is a good thing for the most part). You want to listen for changes on the view stack's selected child anyway just add a listener to that instead and do you're logic in there if you want it every time the view is changed. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/containers/ViewStack.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2#event:change alternatively you can probably use the show event within the object that's in the view stack to get the event when it changes from invisible to visible.
To answer your specific question, by default, each view will get instantiated and initialized once when they are initially shown (same with creation complete.) After that, switching between the views will not result in them being initialized again. If you want to perform some sort of action whenever the selected view changes, you should attach a listener to your ViewStack for the mx.events.IndexChangedEvent.CHANGE event. It would also be helpful to learn more about the flex component lifecycle (which includes initialization). Here's a diagram I have found useful: http://danorlando.com/?p=122
Hope that helps.
I'm still too short on information to answer this completely what is the end goal, to have a view stack on the stage and know when the selected index changed you just do it like this:
<mx:ViewStack id="viewstack" width="100%" height="100%" change="changeHandler(event)">
<s:NavigatorContent id="view1" label="view1">
<views:view1 />
</s:NavigatorContent>
<s:NavigatorContent id="view2" label="view2">
<views:view2 />
</s:NavigatorContent>
</mx:ViewStack>
in the changeHandler function you can use the event.selectedIndex property to know which one of the items is showing right now... or else you can look at the view1.visible property. You can programatically change the one being shown by doing viewStack.selectedIndex = 0 or viewStack.selectedChild = view1.
Also if you want to know from within the component (like within view1's mxml) you can use the show property such as:
<views:view2 show="showHandler(event)"/>
The show() event of a component is dispatched when you pass from invisible to visible, so the following code DOES NOT work for a component contained in a viewstack
<s:NavigatorContent id="view1" label="view1">
<views:view2 show="showHandler(event)"/>
</s:NavigatorContent>
In a viewStack component the simple way is to manage the change event take the following MXML
<mx:ViewStack id="viewStack" change="onViewStackChange(event)">
<s:NavigatorContent id="view1" label="view1_lbl">
<views:view1 name="v1"/>
</s:NavigatorContent>
<s:NavigatorContent id="view2" label="view2_lbl">
<views:view2 name="v2"/>
</s:NavigatorContent>
</mx:ViewStack>
with this ActionScript Code
protected function onViewStackChange(event:IndexChangedEvent):void
{
// alert label of selected nav content
Alert.show("View Stack Change, Selected = " + viewStack.selectedChild.label);
// get selected nav content then show name of first child
var navContent: NavigatorContent = (viewStack.selectedChild as NavigatorContent);
Alert.show( "Change, 1st child is: " + navContent.getChildAt(0).name );
}
You can discriminate multiple components by labels
Cheers
main.mxml
<mx:ViewStack id="viewstack" width="100%" height="100%">
<s:NavigatorContent id="view1" label="view1">
<views:view1 />
</s:NavigatorContent>
<s:NavigatorContent id="view2" label="view2">
<views:view2 />
</s:NavigatorContent>
精彩评论