The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Windows phone app - Deactivated , Closing , activated event handlers RRS feed

  • Question

  • How do i set up events in my Maingage.xaml , to know when the app has been Deactivated , Closed ?
    Tuesday, November 12, 2013 11:39 AM

Answers

  • Let's say your user is on PageX.xaml. When he or she taps Start it goes like this:

    1. OnNavigatingFrom - This is method override is invoked first. At this point you get a chance to cancel the navigation.

    2. OnNavigatedFrom - This is your last chance to save Page-related state. This is the point where you need to save any information that you passed to StreamSocket and then to destroy the object.

    3. Deactivated event is raised - This is your last chance to save App-related state, like cached data from web service, etc.

    Now your application is on the backstack. If you return back to your app the steps are:

    1. Activated event is raised - This is your chance to read any app-related state that you stored earlier.

    2. OnNavigatedTo - Now you can restore any Page-related state. At this point read the information needed for StreamSocket and create the object of type StreamSocket.

    3. OnFragmentNavigation - In case you are using fragment navigation this method override will also be called.


    Huske

    • Marked as answer by kalel111 Wednesday, November 13, 2013 10:39 PM
    Wednesday, November 13, 2013 7:13 PM
  • It is the same regardless of whether you tap Start, Search, press and hold Back to invoke task manager and choose another app, or you invoke Task or Launcher, or you request another app to open a file from your app. In other words, whenever your app is replaced in the foreground by another app this process will take place.

    Huske


    • Edited by Huske Wednesday, November 13, 2013 8:38 PM
    • Marked as answer by kalel111 Wednesday, November 13, 2013 10:39 PM
    Wednesday, November 13, 2013 8:38 PM

All replies

  • Hi,

    in App.xaml.cs you can find these handler

    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
    }

    private void Application_Closing(object sender, ClosingEventArgs e)
     {
     }

    Add your code inside.

    Cheers

    Tuesday, November 12, 2013 11:43 AM
  • I need to be able to access my objects that are in my MainPage.xaml.cs 

    for ex: i have a StreamSocket object in there.  How do i access it from App.xaml.cs ?

    Tuesday, November 12, 2013 11:46 AM
  • In MainPage.xaml.cs, you should use the method OnNavigatingFrom(NavigatingCancelEventArgs e).

    It is invoked when the Page is unloaded

    Tuesday, November 12, 2013 11:52 AM
  • Sorry, i am new to this.

    How does that method allow me to access and use my  StreamSocket object that was created in MainPage.xaml.cs, from Apps.xaml.cs?

    Tuesday, November 12, 2013 11:57 AM
  • You don't really need to worry too much about Deactivated event if StreamSocket is all that you are working with. Let's say that in the middle of some work in you app, a user tape Start or Search. The OnNavigatedFrom method in MainPage.xaml.cs will execute first. At this point you should store any information that you used to construct StreamSocket, like URL or some other information that can be serialized. After you do this you will need to destroy StreamSocket object as it cannot be stored in page state. After this is completed Deactivated event will be fired. If there is any globally needed information that should be stored between app runs and which is transient, you should store it here (if it is not too large).

    When a user returns to this page by pressing Back button, you need to reconstruct the StreamSocket inside of OnNavigatedTo and pass it any information that was previously used. After you do this part you are ready to start using the StreamSocket again.

    The reason that you have too do all this is because you cannot really store StreamSocket in your page state. You can store serializable data types like strings (probably url to which you have connected). StreamSocket is not serializable. So, if your StreamSocket is a global variable, you will need to instantiate it again when a user returns back to your app.

    That's about it.


    Huske



    • Edited by Huske Tuesday, November 12, 2013 12:37 PM
    • Marked as answer by kalel111 Tuesday, November 12, 2013 3:54 PM
    • Unmarked as answer by kalel111 Tuesday, November 12, 2013 3:54 PM
    Tuesday, November 12, 2013 12:24 PM
  • I put this code in  my app: but the code doesn't run when i press the back button on the phone

            protected override void  OnNavigatingFrom(NavigatingCancelEventArgs e)
            {
                e.Cancel = true;
    
                Dispatcher.BeginInvoke(() =>
                {
                    showError("leaving app");
    
                });
            }
    

    Wednesday, November 13, 2013 2:28 AM
  • rather than trying to invoke a UI when backing out, just output a debug string and see if that runs.

    http://pauliom.wordpress.com

    Wednesday, November 13, 2013 5:46 AM
  • Kalel, could you help us understand what is it exactly that you are trying to achieve? We might be able to point you in the right direction if we understand your problem.

    Huske

    Wednesday, November 13, 2013 9:20 AM
  • I have a streamsocket that i want to call the Dispose() method on when the user Leaves the app, by pressing the back button or by pressing the windows logo button. But the socket shouldn't close if they are only using a photo chooser task and decided to press the back arrow or anything similar to this. Only for when the app is closing.
    Wednesday, November 13, 2013 12:14 PM
  • I'm not sure you can distinguish between 'windows logo' and a launcher?

    http://pauliom.wordpress.com

    Wednesday, November 13, 2013 3:05 PM
  • I agree with pkr2000. The app will receive deactivated event when you call a launcher or task. There is not much you can do here at the moment. After all, if you allow your users to choose a photo from your album, why not start StreamSocket after that action, or have users select all the photos they need and then start StreamSocket.

    Huske

    Wednesday, November 13, 2013 3:30 PM
  • Can i somehow know the origin of the being event fired off?

    So i can respond to it appropriately.

    for ex: if it was fired off from the user pressing Start or Back button or from a Task.

    Wednesday, November 13, 2013 4:09 PM
  • Also,

    Just so i know. What navigation events are fire off, when the user Presses the Start Button and the Back Button, and search button?

    Wednesday, November 13, 2013 4:14 PM
  • Let's say your user is on PageX.xaml. When he or she taps Start it goes like this:

    1. OnNavigatingFrom - This is method override is invoked first. At this point you get a chance to cancel the navigation.

    2. OnNavigatedFrom - This is your last chance to save Page-related state. This is the point where you need to save any information that you passed to StreamSocket and then to destroy the object.

    3. Deactivated event is raised - This is your last chance to save App-related state, like cached data from web service, etc.

    Now your application is on the backstack. If you return back to your app the steps are:

    1. Activated event is raised - This is your chance to read any app-related state that you stored earlier.

    2. OnNavigatedTo - Now you can restore any Page-related state. At this point read the information needed for StreamSocket and create the object of type StreamSocket.

    3. OnFragmentNavigation - In case you are using fragment navigation this method override will also be called.


    Huske

    • Marked as answer by kalel111 Wednesday, November 13, 2013 10:39 PM
    Wednesday, November 13, 2013 7:13 PM
  • Is that process the same when a photo chooser task is launched? or when they press the back button?
    Wednesday, November 13, 2013 7:48 PM
  • It is the same regardless of whether you tap Start, Search, press and hold Back to invoke task manager and choose another app, or you invoke Task or Launcher, or you request another app to open a file from your app. In other words, whenever your app is replaced in the foreground by another app this process will take place.

    Huske


    • Edited by Huske Wednesday, November 13, 2013 8:38 PM
    • Marked as answer by kalel111 Wednesday, November 13, 2013 10:39 PM
    Wednesday, November 13, 2013 8:38 PM