Exit app and constructor no longer called on Windows Phone 8 - windows-phone-8

I have a constructor with data to be loaded when the Application run for the first time. However when I press back to exit my application, I can no longer load the constructor once I launch the application again. However this problem will not exist when I press the window button and proceed to select my application. The constructor data will still be loaded for this scenario:
public MainPage()
{
InitializeComponent();
loadData();
}

It is because some information is saved in the memory.
So if you want to call it everytime you login subscribe to the loaded event.
So in your construvtor write this.loaded += and what you want this will allways be called.

Related

WinRt: Suspend and Resume - when should I release my resources and allocate it again?

My WinRt app implements a simple HttpServer. That server gets started when the app starts and should be terminated when the app ends. The HttpServer receives some callbacks from an other remote third party component. I need to register und unregister my HttpServer at the third party component.
My problem here is the Application lifecycle of an WinRt App especially the situation suspend -> resume (without Terminated or ClosedByUser).
In that situation the "OnSuspending" method is called while suspending, but nothing gets called when resuming.
In my App I unregister the HttpServer from the third party component during "OnSuspending".
The question is what gets called so I can reregister my HttppServer at the third party?
Any help is much appreciated!
There is a Resume event, define it in your App.xaml.cs constructor
public App()
{
InitializeComponent();
Suspending += OnSuspending;
this.Resuming += On_resuming;
}
And write your Resume event
private async void On_resuming(object sender, object e)
{
}
Hope it helps

Flash AS3 Do nothing until loaded

I have this program, that needs to load a text file, which it can do, but since everything is multi-threaded on AS3, it continues with the program even though its not loaded. Like, if I was to make a function that loads the text, and then have it return the loaded text, it will always return "undefined". I have to add an event listener for when it's done, but the way I have to handle the returning of the text is awkward. Is there any way to make the program just wait, or do nothing until it's loaded?
It sounds like what you're looking for is for the data to load synchronously so that you can just make the loading call and have it return right away, like so:
# (this actually is not actionscript)
fileDataContents = open("file.txt", "r");
then have the fileDataContents immediately available to use.
By design this is not how flash works.
Loading external data in flash is not multithreaded, but rather asynchronous. One very good reason why synchronous loading is not done is that it causes blocking/locking of the program during the operation potentially resulting in a poor user experience.
Take for example, if this is loading a file over the web: what if the user's internet connection had just cut out/hiccupped or had been moved/deleted/modified suddenly on the server? Or what if the file is moderately sized but the user was on dial-up?
This is out of your control in most cases and the resulting behaviour may be that it causes flash to "forever" freeze in the user's browser window as it tries to load, possibly taking down the whole browser with it, ultimately ending in a poor user experience. By blocking/locking the program in that loop you would have no way to recover or respond to the user appropriately.
No, you can't.
Listening for the COMPLETE event like you have now is the correct way to wait and continue the application flow once done. I can't see how this would become awkward - I suggest updating your question to include information about what's making you uncomfortable about the event system so that I can help you from there.
Perhaps you're just not structuring your code in a way that works well with the event system. Here is how you can set up a very clean, straightforward way of loading some data and starting up the application once done.
Here's the document class, which can be used purely as a channel to load your text data which will be forwarded on to a fresh instance of the actual Application class:
public class Main extends Sprite
{
private var _application:Application;
private var _loader:URLLoader;
public function Main()
{
var fileUrl:URLRequest = new URLRequest("file.txt");
_loader = new URLLoader(fileUrl);
_loader.addEventListener(Event.COMPLETE, _textLoaded);
}
private function _textLoaded(e:Event):void
{
_loader.removeEventListener(Event.COMPLETE, _textLoaded);
// Load the main Application with the required text data.
_application = new Application( _loader.data );
}
}
And then your Application, which you can consider your central class.:
public class Application
{
public function Application(textData:String)
{
// Here we have access to the required text, and we can
// begin preparing the main Application.
trace(textData);
}
}

Getting values from JTextField on buttonclick event

I am writing a simple program which takes 3 values from user using 3 JTextField and with 2 buttons, one for Chart and other for Graph.
On Click of any of this button, the values taken from the interface, should be returned to the calling function is the requirement.
(i.e. I am calling View from Controller and taking values from user in view and expecting back in controller)
For this i have used textfield1.getText() function inside:
Button1.addActionListener(new ActionListener() {
//Overriding function over here for getting the data
});
event. and at the end I am retuning the ArrayList of values taken from user.
However the issue, is as soon as the program i starting to run, it's not waiting for the button click but directly returning the ArrayList to the Controller with the default values.
Appreciate any quick help on this..
I am pretty new to listeners and that might be the reason for the issue..
The question lacks some code to really pinpoint the problem, but I am under the impression you think that the call
do A
button.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
do C
}
} );
do B
will stop your code execution until the button is pressed. This is incorrect.
In the above snippet, it will do A, then attach the listener to the button, and immediately continue with B. The listener code will only be triggered when the button is pressed. So C is only executed when the button is pressed, which is after B.
There is a tutorial about ActionListeners available on the Oracle site. If you are not familiar with the listener concept as you claim, it might also be helpful to read about the Observer design pattern, which is the design pattern you use when you attach a listener.

ActionScript-3 timing issue / unwanted background multi-threading

I created a new class with private vars and public get properties.
When I create a new instance of the class, it loads text files content to the private vars - it probably takes a bit of time to load it.
After the new instance created, I try to get the value of the private var with the get property:
var item1:MyItem = new MyItem("0001");
trace(item1.ItemName);
Well, The output is blank.
The string that ItemName points to is not undefined, it contains data.
So, it's like a timing issue, and ActionScript is probably running the code using background multi-threading, so it's calling the trace command before it finished to run all methods in the MyItem c'tor (methods that load the text file data into the String var that ItemName points to).
Is there any way to force ActionScript avoid using this unwanted "background multi-threading", and run the code normally (by the order of the commands)?
I mean like "Don't run the 2nd command until you finished running the 1st one".
Thanks for any help..
Freddy
It's not multi-threading, loading a text file externally is just a completely asynchronous operation. Within your MyItem class, you need to have an Event.COMPLETE handler for that Loader. From there, there here is what I'd do:
Option 1: In MyItem's COMPLETE handler for the file load, set a flag. The class that uses the getter must check the flag via another getter to see if the data is there, and either use it (if it's there) or set up a listener to wait for it (if it's not). Once the data is loaded, you have immediate synchronous access to it (it's cached).
Option 2: An alternative to keeping the "isLoaded" flag within MyItem would be to have MyItem's Loader COMPLETE handler dispatch it's own TEXT_LOADED event. Then, the instance responsible for creating the MyItem instance would listen for that, and know not to ask for the contents of that text file until it was there.
Either approach will work. It is up to you to figure out which one makes sense. My first option potentially avoids an unnecessary listener if you don't expect to access the loaded data right away (i.e. you're loading the text at startup for use sometime later). The second approach makes sense if you do expect to make use of the text file's data the instant it loads.
Ideally what you'd want to do is implement the Observer Pattern:
Add an event handler to your MyItem class for the Loaded event of whatever component/class you're using to load your XML.
Add an event to your MyItem class that will bubble the Loaded event from Step 1 so that clients of your MyItem class can attach a handler to it. Fire this event inside the Loaded handler you created in Step 1.
Attach an event handler to the event you created in Step 2 and place your trace code in that handler.
Your code is in fact running in a single thread, however the typical data loading classes in Actionscript are asynchronous. It is likely that the data has not loaded before you call trace. To be notified of when the data is loaded you should use an event listener on the loading object.
var loader:URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, onLoadComplete );
loader.load( new URLRequest( 'path_to_data.xml' ) );
function onLoadComplete( event:Event ):void
{
trace( loader.data );
}
The AS3 API docs have a more complete example.

e.getWindow().dispose() is not working?

I have a main program which calls a JFrame to get User information, If a user press submit I am storing the information in POJO and getting it into Main program.
If User clicks on Exit, I want to dispose the JFrame and want to exit the main program as well after executing one logging statement in main method.
If I use -
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
e.getWindow().dispose();
}
});
all the Threads exit immediately and I am not able to execute logging statments in main method.
If I use-
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
e.getWindow().dispose();
}
});
My main method execute those logging statments but it never exit, It stay silent after executing all the statements.
If I use this -
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
Thread.currentThread.interrupt();
}
});
Everything works fine. But is it the correct way of doing this?
In theory, if you dispose all top-level windows, the JVM should terminate cleanly.
However, there are a few details to make sure, which are detailed on this page:
Therefore, a stand-alone AWT
application that wishes to exit
cleanly without calling System.exit
must:
Make sure that all AWT or Swing components are made undisplayable when
the application finishes. This can be
done by calling Window.dispose on all
top-level Windows. See
Frame.getFrames.
Make sure that no method of AWT event listeners registered by the
application with any AWT or Swing
component can run into an infinite
loop or hang indefinitely. For
example, an AWT listener method
triggered by some AWT event can post a
new AWT event of the same type to the
EventQueue. The argument is that
methods of AWT event listeners are
typically executed on helper threads.
If you don't manage to find what keeps the program alive, there can be another way:
-Use the first method you described, with the EXIT_ON_CLOSE
-Add a ShutdownHook to the runtime, to execute your last logging statements:
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
//logging statements
});
Dispose method only releases system resources.
To actually close the window you have to call setVisible(false)