0

I know that for iOS, we can observe notification UIApplicationWillTerminateNotification when app is terminating (see this SO). On windows phone, is there a similar mechanism so that I can register a event to release some app resources or saving some objects to the database?

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234

2 Answers2

2

You need to handle the Suspending event. This will give you about 5-10 seconds to finish up any tasks. Just remember to take the deferral from the event args if you do any async work.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • In some rare cases it won't work - the *Suspening* event won't be fired. In such cases *Window.VisibilityChanged* may be a better option, though it may have problems with running longer operations, due to app termination. – Romasz Aug 05 '15 at 05:35
  • Visibility changes for things like an incoming phone call that don't actually suspend your app. Also if you move to desktop then your visibility might change for various reasons. If you have cases where Suspending isn't raised, please let me know and I'll log bugs. – Peter Torr - MSFT Aug 05 '15 at 06:15
  • You are right, seems like the case I've been facing some time ago, now, as I've checked, works just all right. – Romasz Aug 06 '15 at 12:11
1

Talked to one of my colleague just now, this may be a way to do it:

    protected override void OnWindowCreated(WindowCreatedEventArgs args)
    {
        args.Window.VisibilityChanged += WindowVisibilityChanged;
    }

    private void WindowVisibilityChanged(object sender, VisibilityChangedEventArgs e)
    {
        if (!e.Visible)
        {
            // Add the code here
        }
    }
Yuchen
  • 30,852
  • 26
  • 164
  • 234