I am moving from Windows Phone 8 to Windows Phone 8.1.
I Created a Windows Phone 8.1 Store Application, Hub App.
The application created the OnNavigatedTo and OnNavigatedFrom methods
protected override void OnNavigatedTo( NavigationEventArgs e )
{
this.navigationHelper.OnNavigatedTo( e );
}
protected override void OnNavigatedFrom( NavigationEventArgs e )
{
this.navigationHelper.OnNavigatedFrom( e );
}
I put a breakpoint in the OnNavigatedFrom and tried to either close the application, or to leave the application and the breakpoint is not hit, i.e. the application doesn't reach the OnNavigatedFrom.
A Windows Phone 8 application is breaking on the OnNavigatedFrom. Is the mechanism is different with WP 8.1? if so how?
Thanks.
The problem seems to occur, because you are running in Debug mode (VS attached). In this situation your program behaves little different in case Navigation/Suspend events, to test it properly you will have to invoke the Suspending event manually (Lifecyce events dropdown). In normal situation both events (OnNavigatedFrom and Suspending) will be called just after you leave the app.
To test it let's put something in OnNavigatedFrom (basing on Hub App from Windows Store templates):
protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedFrom");
Hub.Background = new SolidColorBrush(Colors.Red);
this.navigationHelper.OnNavigatedFrom(e);
}
in this case, when you run the app without Visual Studio attached, when you return to the app the background should be red - which means that the event has been fired.
There is, in fact, one more huge (IMO) difference when moving to WP8.1 WinRT - OnNavigatedTo won't be fired when you come back from suspension:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
it's called only when you navigate.
Some more references: Navigating between pages, Lifecycle, Launching, resuming, and multitasking and Guidelines for app suspend and resume.
Related
We have a Xamarin UWP app that needs to be logged out whenever a user minimizes or clicks away from the window.
In my App.xaml.cs I have registered an event handler for the Suspending event. I then put our logout code in this event handler like so:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.GetDeferral();
AppBackgrounded();
deferral.Complete();
}
This AppBackgrounded() method looks like this:
void AppBackgrounded()
{
if (!_isInBackgroundMode)
{
_isInBackgroundMode = true;
if (UserSetPin)
{
PinPage passcodePin = new PinPage();
Navigation.PushModalAsync(New NavigationPage(passcodePin), false);
}
else
{
App.Logout(null, true, true);
}
// clears the pasteboard so data can't be copied from this app into other apps
Clipboard.Clear();
}
}
We also have a AppLeavingBackground method that we use to restore the app when the user returns, but the app does not crash when returning. It only crashes when running the OnSuspended method.
This crash only occurs when
The App is built for release and
The device is in tablet mode
When in tablet mode, if you press the Task View button and navigate to another application the UWP app freezes trying to run through this code. If you try to return to the app, it will immediately exit.
I have tried to make the navigation to the other pages async and the app will then crash even when its not in tablet mode. I have also tried to put this logic in AppEnteredBackground and it still occurs.
This is hard to debug since it only occurs in release mode. Any ideas?
In my case it was the Clipboard.Clear() function that was crashing the application. For those encountering similar issues, checkout the other answers as they all provide great points.
As a side note, I also found that using async code in these events was crashing my application. I'm not sure why since I was using the deferral, but since it's working i'm not going to pursue it further.
I cannot say that I have read exactly the documentation that says that navigating the pages in suspending will result in a crash, but it is clear that this is the wrong place to do it.
The suspending is not used to prepare your app to be opened again, the whole reason for this lifecycle event is that you need to prepare your app for not being open again, which means saving some data that may eventually get lost. Preparing your app to be opened again is done in resuming.
I have big trouble finding right event for me - user during browsing files in my app [UWP Windows 10 Mobile app] can tap on it and then I launch it in default app by
Windows.System.Launcher.LaunchFileAsync
my app is 'minimzed' (just like by pressing Windows key) and user can interact with file in whatever app he wants. Now by pressing back-key he is returning to my app. Do you know any event which is trigerred now? I want to update the file (if it was changed) but I cannot find any event to check it.
Take a look at App lifecycle, I think you should register event handler for both OnLaunched and Resuming, one for switching back from terminated state, another for from suspended state.
When the user switches back to a suspended app that has been terminated, the app should restore its application data in its OnLaunched method.
If an app registered an event handler for the Application.Resuming event, it is called when the app is resumed from the Suspended state. You can refresh your app content and data using this event handler.
It is important to understand the UWP app lifecycle.
If you want to have control on what is happening while application is Launched first time, Suspended or Resumed you should refer to below guideline:
https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle
Also in the App.xaml.cs file you can manage this cycle. For instance you can control what to do when application is Resumed from the background:
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.Resuming += App_Resuming;
}
private void App_Resuming(object sender, object e)
{
//Code to execute while resuming the app...
}
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).
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
This is in a Windows Phone 8.1 Store app. My MainPage has a CaptureElement to display the preview stream from my MediaCapture object. For navigation within the app (between pages), this works well:
MediaCapture mc;
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
mc = new MediaCapture();
await mc.InitializeAsync();
preview.Source = mc;
await mc.StartPreviewAsync();
}
protected override async void OnNavigatedFrom(NavigationEventArgs e)
{
await mc.StopPreviewAsync();
}
I can navigate to other pages and come back, and the preview runs reliably. I'm running into problems for the following scenarios though:
User presses the Windows button, then the back button
User presses the Windows button, then uses the task switcher to come back to my app
User presses the search button, then the back button
User presses the power button, then presses it again and swipes up to unlock the device
User holds down the back button to enter the task switcher, then taps on my app again
After each of the above actions (and/or combinations of them), when my app comes back the preview is frozen at the last frame that was displayed.
If the user then navigates to a different page and then back to the MainPage, the preview starts running again without an issue, so this leads me to believe I just need to stop/start the preview after coming back from one of the above scenarios.
I tried subscribing to the App.Suspending and App.Resuming events, but these don't trigger on these occasions. What am I missing?
You will have to use App.Suspending and App.Resuming (for the cases you have described) with combination of Navigation events (when navigating between Pages). The OnNavigatingFrom event is called when you hit Start, hold Back or use Search (when the App is being suspended), but when you resume the App, OnNavigatedTo is not being called - this event is called only when you are navigating. So in your case, when you hit Start, the preview stops and when you come back it doesn't start again. A refference to MSDN:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
The other thing is that to debug the App properly you will have to use Lifecycle Events of Debug Location tab in Visual Studio - while you are debbuging the app, it is not being suspended, but when you run your app normally, it gets suspended just after you hit Start.
Note also that the App can be put into Not Running state. More about Lifecycle at MSDN.
The scenarios you described should trigger the Window.Current.VisibilityChanged event where you can use VisibilityChangedEventArgs.Visible passed into the event handler to cleanup preview when not visible and initialize preview when visible. You can subscribe\unsubscribe to Window.Current.VisibilityChanged event in your Loaded\Unloaded handler for your Page\UserControl.
The reason why Suspend/Resume lifecycle events is not sufficient is because the scenarios you mentioned above doesn't deterministically invoke those events at a certain time as the OS will only suspend an app based on an internal policy that can change with OS release updates.
Also as an aside, I would avoid using Navigation handlers and instead rely on Loaded\Unloaded handlers which will allow initialization\cleanup to occur properly if you ever had to move your CaptureElement into its own UserControl as opposed to in a Page and avoids the scenario where WP will call OnNavigatedFrom and not call OnNavigatedTo for suspend\resume (Loaded\Unloaded will always be called in order).
I'm attempting to execute some code on my Windows 8.1 app on exit, when a user drags the app down normally (not the extended hold to terminate).
In Windows 8, I used the following code to clear the app's tile on exit:-
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication().Clear();
}
However, this code isn't called in Windows 8.1 when the way the app terminate changes.
How do I recreate my code for Windows 8.1?
In 8.1, the close gesture no longer kills the app. Instead you have to hold it at the button for 1 second and when the icons flips, the app is closed.
Look if the code is executed there.