0

I am developing application in windows phone 10

For some reason, I must handle application state (go to background, enter foreground). I have handle event suspend and resume at App.xaml.cs but it does not works, OnSuspending and OnResuming are not reached. Please help me check my source code and show me how to handle those events.

Here is my code:

public App()
    {
        Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
            Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
            Microsoft.ApplicationInsights.WindowsCollectors.Session);
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);
        Application.Current.Resuming += new EventHandler<Object>(OnResuming);

    }

    private void OnSuspending(Object sender, Windows.ApplicationModel.SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        //TODO: Save application state and stop any background activity
        deferral.Complete();


    }
    private void OnResuming(object sender, object e)
    {
        // do some thing
    }
JJJ
  • 32,902
  • 20
  • 89
  • 102
ThangBM
  • 301
  • 3
  • 10
  • If you try to debug it, normally you won't fire those events - you will have to use *lifecycle tab* - [see this question](http://stackoverflow.com/q/24103101/2681948) for more help. – Romasz May 12 '16 at 14:01

2 Answers2

1

You should use the Lifecycle Events dropdown of Visual Studio 2015. It lets you choose between Suspend, Resume or Suspend and Shutdown states.

Whenever you run your app in debug, it never goes to suspended state on its own.

Some doc here: https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/

1

You are subscribing to suspend event twice

 this.Suspending += OnSuspending;
 Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);

better leave classic

 this.Suspending += OnSuspending;
 this.Resuming += App_Resuming;

And also same way add resuming event

 private void App_Resuming(object sender, object e)
    {
       // TODO
    }

Do you debug is suspend/resume event works like it's described in this article:
How to trigger suspend, resume, and background events for Windows Store apps in Visual Studio

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25