locked
Distinguish between resuming from a previous session and starting over

    Question

  • Hello,

    while trying out an example of the MOC20484 course (Essentials of developing Windows store apps using c#), I'm struggling to get the following example working. Basically it tries to distuinguish whether an app is activated from a terminated or suspended state. My problem is that the OnActivated does never seem to be called when the app is launched.

    When putting a breakpoint on the switch statement, it doesn't break on app launch, or resuming from suspension (using Visual studio forced suspend/resume buttons).

    I'm wondering how to get this to work and I'm rather confused that I get such an easy example not working from the course book.

            protected override void OnActivated(IActivatedEventArgs args)
            {
                switch (args.Kind)
                {
                    case ActivationKind.Launch:
                        {

    // is never called????

                            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                            {
                                // start over
                            }
                            else
                            {
                                // return from suspension
                            }

                            break;
                        }
                    case ActivationKind.File:
                        {
                            // other logic
                            break;
                        }
                    default:
                        break;
                }
                base.OnActivated(args);
            }





    • Edited by Edsger Wednesday, April 2, 2014 10:16 AM
    Wednesday, April 2, 2014 10:11 AM

Answers

  • If your exam doesn't follow documentation, and the code doesn't do what the examsays, then you must conclude that the exam is incorrect.

    Use the proper launch/activation methods and you'll be fine.  test them to see if they work like they are supposed to.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Edsger Monday, April 7, 2014 5:25 PM
    Thursday, April 3, 2014 2:06 PM
    Moderator

All replies

  • If you don't hit a BP on the switch statement, it's likely that you don't have an event handler wired up to the OnActivated method.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, April 2, 2014 1:29 PM
    Moderator
  • It's not an event. This is an overidden method of the Application class.

    See also MSDN documentation.

    Wednesday, April 2, 2014 1:38 PM
  • OK my mistake. But the documentation does describe when the method should be called:

    When a user launches your app normally (for example, by tapping the app tile), only the OnLaunched method is called. Override the OnActivated method to perform any general app initialization that should occur only when the app is not launched normally (for example, from another app through the Search contract). You can determine how the app was activated through the IActivatedEventArgs.Kind property.

    This means that it's not called on launch. For launch, you want the "OnLaunched" method, and for resuming, you want to handle the "Resuming" method.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, April 2, 2014 1:50 PM
    Moderator
  • Yes, I also read that on the documentation page. However, this is confusing as my official Exam documentation for developing Windows store apps explicitly states otherwise and it also has a code example that shows how to use it. Next to that, having several entry point in your code for handling different activation kinds is horrible. To give an indication, when I implement my OnActivated method I use a switch to identify how my app  is activated like

    switch(activationmethod.EnumValue) {

    case enumValue1:

    case enumValue2:

    //case enumValue3: <= must be handled in different method?? 

    }

    however, if I follow the documentation on the page, I must handle different values of the enum in different methods?! I hope this is not the case, because this is horrible code to write and its about a fundamental part of the application. 

    Wednesday, April 2, 2014 2:14 PM
  • If your exam doesn't follow documentation, and the code doesn't do what the examsays, then you must conclude that the exam is incorrect.

    Use the proper launch/activation methods and you'll be fine.  test them to see if they work like they are supposed to.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Edsger Monday, April 7, 2014 5:25 PM
    Thursday, April 3, 2014 2:06 PM
    Moderator
  • In the end I think the exam books are really bad. They are full of errors and were probably created when either the code was not yet finished, or they were simply not tested very well.
    Wednesday, August 13, 2014 10:14 PM