Page constructor called multiple times - Windows Phone 8 - windows-phone-8

I have a page in a WP8 application, that every time I navigate to it, the constructor is called.
From what I know, the constructor of a page called only once at the first time the page loaded. my page is very heavy, and every construction takes wasted time..
this my navigation code, usual one:
NavigationService.Navigate(new Uri("/Views/Pages/ContentControlNew.xaml", UriKind.Relative));
and this is the constructor of the page:
public ContentControlNew()
{
InitializeComponent();
}
Not special.. is it normal that the constructor is called every time? Please tell me if you need more details because I don't know what else to say about this subject.

Yes this is normal because whenever you use NavigationService.Navigate it always creates a new page object and adds that (pushes it) to the navigation stack. For example when you use GoBack() it pops it out of the stack and destroys it, but when it gets back to the previous page it doesn't call the constructor since that one was already in the stack and does not have to be recreated.
If you don't want to create a page every time you navigate to it, you should look into Navigation Models for Windows Phone for some ideas on how you can tackle this.

Related

Handling a keyboard event in one place for the whole program

I'm creating a little developer console for an AS3 AIR application, I'm wanting F12 to add the toggle the display of the console screen but I don't want to litter my program with a bunch of calls to the Console to show or hide it, I also don't really want to be re-creating the console on different screens of my application.
I'm wondering if there's a way or a place I can put my keyboard event to toggle the display that will handle it across the entire application? At the moment I've tried putting it into my Main class which calls the first screen in the hopes that would be able to handle it but as soon as I click on another screen my eventListener isn't called.
Any ideas?
You could add your event listener to FlexGlobals.topLevelApplication instead of specific views, this would achieve the reduction you require
For true application level keyboard handling, attach the listener on the NativeApplication.nativeApplication object.
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, toggleDevConsole,false,0,true);
Attaching the listener to the stage will only work when that particular stage (window) has the focus. This will become an issue if your application has multiple windows that require interaction.
For single window applications, either will work.
Woops, I'm not quite with it today!
For future reference I added the event listener to the Stage in my Main function and it's being picked up every time.
stage.addEventListener(KeyboardEvent.KEY_DOWN, toggleDevConsole, false, 0, true);

iOS 8 today widget stops working after a while

I've made a today widget for the german ice hockey league DEL.
I'm loading the next games from our server an show them in a tableView. The loading process is started in the proposed method "widgetPerformUpdateWithCompletionHandler". Initially i'm loading some cached data in "viewWillAppear".
Everything works great so far!
But after a while (one day) the widget stops working. When I open the notification center the widget appears normal, but it is never updated again. I have to remove the widget from the notification center and have to add it again. After that the widget works for a day and then again it stops working.
To see what the widget ist doing, I've added a simple white view with a status text above the table view while loading the data in "widgetPerformUpdateWithCompletionHandler" to see if the widget is doing anything. The white view appears when the widget is working. When it is not working the status view doesn't appear. So I think the method "widgetPerformUpdateWithCompletionHandler" isn't called after the widget is active in the notification center for a while.
I've got no clue what causes the widget to stop working. Any ideas?
I've got the same problem and I resolved it by calling completionHandler(NCUpdateResultNoData);
right after your network request even when the response hasn't been returned. I found that if completion handler is not called the widgetPerformUpdateWithCompletionHandler
will no longer get invoked, and therefore there won't be any more updates. Also make sure you call completion handler in all branches after your request call returns.
As others have mentioned, this is caused by not having previous called the completionHandler after widgetPerformUpdateWithCompletionHandler has been called. You need to make sure that completionHandler is called no matter what.
The way I suggest handling this is by saving the completionHandler as an instance variable, and then calling it with failed in viewDidDisappear:
#property(nonatomic, copy) void (^completionHandler)(NCUpdateResult);
#property(nonatomic) BOOL hasSignaled;
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
self.completionHandler = completionHandler;
// Do work.
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if (!self.hasSignaled) [self signalComplete:NCUpdateResultFailed];
}
- (void)signalComplete:(NCUpdateResult)updateResult {
NSLog(#"Signaling complete: %lu", updateResult);
self.hasSignaled = YES;
if (self.completionHandler) self.completionHandler(updateResult);
}
An additional problem is that once widgetPerformUpdateWithCompletionHandler: is stopped being called, there will never be another completion handler to call. I haven't found a way to make the system call widgetPerformUpdateWithCompletionHandler: again. Therefore, make sure your widget will also try to reload data through viewWillAppear: as a fallback, or some users might be stuck with a non-loading widget.
Calling the completionHandler with NCUpdateResultNewData within widgetPerformUpdateWithCompletionHandler before an async call comes back and calls it again with NCUpdateResultNewData or NCUpdateResultFailed seems to work.

Swing JTabbedPane : addChangeListener or addContainerListener or both?

I have one swing code written by other person. For swing tabbed pane, he has added both change and container listener and both calls the same method:
addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent theEvent ) {
someMethod();
}
} );
addContainerListener(new ContainerAdapter() {
public void componentAdded(ContainerEvent theEvent) {
someMethod();
}
public void componentRemoved(ContainerEvent theEvent) {
someMethod();
}
} );
Whenever tab is removed from this tabbed pane, it internally calls JTabbedPane.removeTabAt(int index), which in turn calls fireStateChanged() causing new change event listened by change listener.
Now as new component (tab) is removed from tabbed pane, it also calls componentRemoved(ContainerEvent theEvent) method of container listener.
Both change even and container events, then calls same method someMethod(), which does set background and foreground colors.
I would like to know, if this kind code might cause some issues. Recently we are facing random IndexOutOfBoundException exeptions. I am just wondering, if this is causing this issue.
Also as per my understanding in swing, once event is listened, logic inside it should be executed using worker thread (e.g. SwingWorker). Please let me know if this is correct.
I am new to swing, thus any hint would be appreciated.
Thanks.
Whenever tab is removed from this tabbed pane, it internally calls
JTabbedPane.removeTabAt(int index), which in turn calls
fireStateChanged() causing new change event listened by change
listener.
This is true if the removed tab is also the selected tab. In the other cases, you won't be notified.
You need to choose what event you want to listen to:
Addition/Removal of components?--> go for ContainerListener
Selected tab? --> go for ChangeListener
I would like to know, if this kind code might cause some issues.
Recently we are facing random IndexOutOfBoundException exeptions. I am
just wondering, if this is causing this issue.
Since there is no line in your sample code that could throw that Exception, it is impossible to answer your question. Post an SSCCE that shows your issue.
Also as per my understanding in swing, once event is listened, logic
inside it should be executed using worker thread (e.g. SwingWorker).
Please let me know if this is correct.
It depends:
If you need to modify anything in the UI, anything related to Swing, it needs to be executed on the EDT (Event Dispatching Thread) and thus, SwingWorker is not an option.
If you need to perform business logic operations, and especially if they can be lengthy, then you should indeed use a SwingWorker or any other mechanism to execute that code in another thread than the EDT. Consider visiting the Swing tag wiki on "Concurrency"

Is there a way in ActionScript to force an IMMEDIATE update of the stage, _not_ after an event?

I know the method MouseEvent.updateAfterEvent() or KeyboardEvent.updateAfterEvent() which will force a re-render of the stage just after the event is handled rather than waiting for the next frame.
However, I need a method to force an immediate render at the very moment I call it. Is there such a method?
Actually my problem comes from the demential design of ActionScript's printing API (PrintJob). Inconsistent with the whole ActionScript architecture, when you call PrintJob.start(), everything is completely frozen while the printing dialog is shown until the user clicks the print or cancel button. Execution of any code after the PrintJob.start() call is resumed after that.
Among a lot of other much worse issues coming from this gigantic design flaw, there is mine:
public function someMouseOrKeyboardEventHandler() {
somethingThatUpdatesTheDisplayList();
var somePrintJob=new PrintJob();
somePrintJob.start();
//...
somePrintJob.send();
}
When this handler of mine is called, the changes made to the display list will not be visible until after the printing dialog has been closed, so I can't, for example, show something on the screen just before I open the print dialog.
updateAfterEvent() won't help a bit (already tried it). It won't change a thing, since it only forces rendering after the event handler code is executed.
Is there any updateRightNow()-like thing?
Nope, you unfortunately can't force an update in the middle of your code.
You can, however, wait until the next frame to call start() on the PrintJob; this will give Flash time to update the stage before everything freezes.

symbols placed on the timeline become undefined if stepping backwards

I am using the frames in the timeline of a .swf as pages in a flash app. The user can advance to the next page by clicking a button that takes her to the next frame. Similarly, it is possible to navigate to the previous frame/page as well.
Most of the content is placed on the stage (i.e. created by dragging an instance of a library symbol to the stage) but properties of those instances, such as .visible might be changed via actionscript. Also, some objects are loaded from external flash files and displayed programmatically with addChild / addChildAt.
The problem is, if I am on Frame N+1 and there is an object displayed on the stage programmatically (i.e. with addChild, not by having it placed on the stage) and navigate to Frame N where there is an object that is placed on the stage (i.e. dragged from the library),
then the instance of that object is undefined/null and throws an error if I try to set its properties (like .visible).
The error does not occur if I am moving to the NEXT frame, only if I am moving to the PREVIOUS one. Therefore I assume that some kind of initialization is not getting called while going one frame back.
I was also thinking that the objects would just not "live" to the next timeframe, that is, their value would be lost and re-initialized because of scope, but if there is no dynamically created object on the stage, I can navigate back and forth just fine.
Is there a way to ensure that the objects created on the stage do not disappear while navigating back to the previous frame?
The first, and more useful, part of the answer is this: timeline keyframes and scripts can give conflicting information about display objects - whether they should exist, where they should be, and so on. For example, when you add an item by playing into its frame, and then delete it with script, and then play into its frame again. When this happens, there's no unambiguously correct thing for Flash to do, so it tends to be unpredictable. I believe what generally happens is that once you fiddle with a given object via script, it's considered to no longer pay attention to the timeline - but your mileage will vary.
Having said that, the reason things are different when you play backwards is the second and more arcane part of the answer. Internally Flash functions differently when seeking forward and backwards on the timeline. Flash internally treats keyframes as changes to be applied in the forward direction, so as you play forward, it applies those changes in sequence. When you move backwards, however, from frame N+X to frame N, it doesn't scan through the intervening X frames reversing those changes - it jumps back to frame 1 and fast-forwards along to frame N. Normally, it amounts to the same thing and you don't need to worry about it, but when you get into the twitchy area where scripts and the timeline have a different idea of what should be on the stage, you're liable to see things behave differently depending on which way you jump (as you are now).
The super-short version is, for things to work predictably, try to ensure that any given object gets added, updated, and removed the same way - either all via script, or all via the timeline. When that seems impossible, fiddle with your content structure - usually, the best solution is to change your object into two nested ones, so that the things you want to do with script occur one level higher or lower than the things you want to do with the timeline.
I'm not sure I got your question right, but as3 does not instantiate elements on the timeline as soon as you gotoAndSomething, but later that frame.
That is, you can't
this.gotoAndPlay(10)
this.elementOnTimelineFrame10.DoSomething()
without errors.
I remember using this chunk of code in the past to work around this problem. It uses the Stage.Invalidate() function to wait for an Event.RENDER before trying to access and children, more info (although vague as hell) is here
private function init():void
{
stage.addEventListener(Event.RENDER, stage_renderHandler);
}
private function stage_renderHandler(evt:Event):void
{
// Run your code here
updateChildren();
}
private function enterFrameHandler(evt:Event):void
{
// triggers the RENDER event
stage.invalidate();
}
This also might me very costly (performance wise). I would strongly advise against dynamically adding/removing objects to an existing timeline, is there any way in which you can place an empty Sprite above the timeline animation and use that for all your dynamic content?
Hope this helps