8

For security reasons, I need to log out the users when they exit the app and show login screen when they return back.

In Windows Phone 8 and Windows Phone 8.1 Silverlight there are Application_Deactivated and Application_Closing methods on the App class (or methods OnClose, OnDeactivate to override in Caliburn.Micro).

The only interesting events seems to be Suspend and Resume, but they do not called when I exit the app using the Start button and get back using the Back button or launching the app from the list.

What are the alternatives for Windows Phone 8.1 XAML?

(Setting ActivationPolicy="Replace" would solve half of the problem but I guess this is not possible, when WMAppManifest.xml is not event a part of a Windows Phone 8.1 XAML project).

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
  • 1
    The *Suspending* event will be called just after you navigate away from the app, but not in debug mode. Have you tested it without debugger attached? – Romasz Jun 16 '14 at 17:38
  • I made it write something to ApplicationData.Current.LocalSettings in Suspending and read on a page, built it in release mode, deploy to a phone, disconnected the cable, run and exited. Not working – Igor Kulman Jun 16 '14 at 17:51
  • Could you repeat that test, but first - restart the phone and reinstall the app (build in Release, with no debugger attached)? That's partially a suggestion, and partially gathering info on a problem I am facing. Thanks. – yasen Jun 16 '14 at 17:55
  • Have you considered a Universal App? – Peter Ritchie Jun 16 '14 at 18:03
  • @PeterRitchie it is a universal app where I just use the windows phone project (although I do not see why this is important) – Igor Kulman Jun 16 '14 at 18:07
  • 1
    Hmm, as I'm testing it's working as I've said - can you check my [example](http://1drv.ms/1justFk). Note also that when Suspending is called, before that OnNavigatedFrom is called, but when you Resume, OnNavigatedTo is **not** called. – Romasz Jun 16 '14 at 18:07
  • @Romasz you are right, reacting to Resuming as you do (after some MVVM conversion) seems to work. Make it an answer – Igor Kulman Jun 16 '14 at 18:42

1 Answers1

15

The Suspending event will be called just after you navigate away from the app, but not in debug mode. I've build a simple app modyfing LocalSettings upon Suspending event and then acquiring information when Resuming.

You are probably aware, but for the sake of completeness of the answer - some remarks:

  • before Suspending event, the OnNavigatedFrom event is being called, but when you Resume, the OnNavigatedTo is not called - reference:

    Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.

  • to test Suspending/Resuming with debugger, use Lifecycle events in Debug location tab - more info

  • reference to Application lifecycle in Windows Runtime apps

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Is there anyway to know in the OnNavigatedFrom if the App is navigating from this page because it is being suspended?? I have event handlers that update the UI, that I dont want them unsubscribed from if I am just suspending the App. – MattyMerrix Jun 08 '15 at 16:23
  • @MattyMerrix I think you should be able to by parsing *NavigationEventArgs* - when app is being suspended (and of course you have a registered frame, use SuspensionManager and so on), then NavigatedFrom event should be called with *Firward* navigation, try to debug and you should be able to decide when was suspension. BUT I don't think it's a good idea to do what you want - remember that suspended app can be terminated, you should subscribe/unsubscribe when you suspend/resume, remember that suspended app is not acting in the background. – Romasz Jun 08 '15 at 20:16
  • if (e.Uri.ToString() != "app://external/") { UnSubscribeFromEvents(); } – MattyMerrix Jun 09 '15 at 14:25
  • This worked great, as you see the NavigationEventArgs Uri property contains app://external/ when you are being suspended, any other time it contains a full uri string of the page you are going too! This is just what I was looking for. I understand that the app cannot be terminated, im not sure what the problem is you are describing? – MattyMerrix Jun 09 '15 at 14:26
  • @MattyMerrix Thanks for the information. As for the problem - when you navigate away, the OS will likely suspend the app within few seconds, it will reside in memory and can be quickly resumed, BUT in some cases especially if phone is low on resources, your app may be termineted by OS and you cannot do much about it. So you must be prepared for that scenario. – Romasz Jun 09 '15 at 14:31
  • Oh yes, this is not mission critical stuff Im doing here, basically we have a few pages, that are Corporate skinned pages, and I want to make sure the UI elements are updated to the correct Corporate colors, so I use a Refresh event to manually update the styles where I need. – MattyMerrix Jun 09 '15 at 14:36