This is in a Windows Phone 8.1 Store app. My MainPage
has a CaptureElement
to display the preview stream from my MediaCapture
object.
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).