1

I do want to ensure that in case an app is navigated to a certain page, the app is on another (in my case the previous) page after it was suspended or terminated. In my case the page is for taking photos. I do not want the user to return to this page after the app was in the background since it has no context information. The context information is on the previuos page.

How could I achieve this with Prism.StoreApps?

Background: If an app was just suspended the state of the app remains after it was resumed, hence the last active page is active again. I have no real idea how to set another page active in this case. If an app was terminated Prim.StoreApps restores the navigation state and navigates to the last active view model (hence to the last active page). I do not know either how to alter the navigation state in this case so that another page is navigated to.

Jürgen Bayer
  • 2,993
  • 3
  • 26
  • 51

1 Answers1

0

In the meantime I fond a working solution myself. Might not be the best and there might be better solutions, but it works.

For resuming the app I handle the Resuming event:

private void OnResuming(object sender, object o)
{
   // Check if the current root frame contains the page we do not want to 
   // be activated after a resume
   var rootFrame = Window.Current.Content as Frame;
   if (rootFrame != null && rootFrame.CurrentSourcePageType == typeof (NotToBeResumedOnPage))
   {
      // In case the page we don't want to be activated after a resume would be activated:
      // Go back to the previous page (or optionally to another page)
      this.NavigationService.GoBack();
   }
}

For the page restore after termination I firstly use a property in the App class:

public bool MustPreventNavigationToPageNotToBeResumedOn { get; set; }

public App()
{
    this.InitializeComponent();

    // We assume that the app was restored from termination and therefore we must prevent navigation 
    // to the page that should not be navigated to after suspension and termination. 
    // In OnLaunchApplicationAsync MustPreventNavigationToPageNotToBeResumedOn is set to false since 
    // OnLaunchApplicationAsync is not invoked when the app was restored from termination.
    this.MustPreventNavigationToPageNotToBeResumedOn = true; 

    this.Resuming += this.OnResuming;
}

protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
   // If the application is launched normally we do not prevent navigation to the
   // page that should not be navigated to.
   this.MustPreventNavigationToPageNotToBeResumedOn = false;

   this.NavigationService.Navigate("Main", null);

   return Task.FromResult<object>(null);
}

In OnNavigatedTo of the page I do not want to be activated on a resume I check this property and simply navigate back if it is true (and set the property to false to allow subsequent navigation):

public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, 
   Dictionary<string, object> viewModelState)
{
   if (((App)Application.Current).MustPreventNavigationToPageNotToBeResumedOn)
   {
      // If must prevent navigation to this page (that should not be navigated to after 
      // suspension and termination) we reset the marker and just go back. 
      ((App)Application.Current).MustPreventNavigationToPageNotToBeResumedOn = false;
      this.navigationService.GoBack();
   }
   else
   {
      base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
   }
}
Jürgen Bayer
  • 2,993
  • 3
  • 26
  • 51