SuspensionManager in Windows Universal Apps doesn't do anything on fast app switching

2k views Asked by At

I'm having troubles understanding how can I execute code when doing fast app switching (i.e. pressing the Windows/Start button to show Start screen on the Phone emulator, and then pressing the Back button to go back into the app).

To simplify the issue, I started a new Windows Universal App that uses the "Visual C# Hub App (Universal Apps)" template as base code (since it includes the SuspensionManager and the NavigationHelper). Since I'm not interested in the Hub itself, I removed all the Grid content from the HubPage.xaml and simply added a TextBox called TimeTextBox:

...
    <Grid x:Name="LayoutRoot">
        <TextBox Name="TimeTextBox"/>
    </Grid>
</Page>

Then, in the HubPage.xaml.cs, I added the following simple line to the method NavigationHelper_LoadState:

private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
    this.DefaultViewModel["Groups"] = sampleDataGroups;

    TimeTextBox.Text = DateTime.Now.TimeOfDay.ToString();
}

If I execute the app on the Phone emulator after applying those simple changes, the app will show the time of the day when loading the page, for example: 16:08:53.4390827.

What I want is that time to be updated every time I navigate to that page. But I if use the Lifecycle Events from Visual Studio to simulate a Suspend, when I send the Resume event the time is still the same: 16:08:53.4390827, and a breakpoint in that line will confirm that the NavigationHelper_LoadState method doesn't get executed when resuming.

The explanation for this is that the App.xaml.cs, as it is in the template, doesn't provide any listener for the Resume event, so nothing gets executed. Adding the next few lines fixes that:

    public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;

        this.Resuming += App_Resuming;
    }

    async void App_Resuming(object sender, object e)
    {
        await SuspensionManager.RestoreAsync();
    }

So if I run again the app on the Phone emulator, now I get the actual time after resuming. Great! The problem is that these Suspend/Resume events don't get triggered when I simply tap on the Windows button of the Phone and then tap on the back button.

Actually, I haven't been able to identify one single method that gets executed when performing that kind of fast app switch. And that's the scenario I'm actually interested for my Universal App.

Is there a way of catching when the navigation brings us back into the app from the Start screen through the Back button? Am I missing any code to handle this scenario?

Thanks!

1

There are 1 answers

0
Heshan On

There's nothing wrong here. It's the default behavior. When you debug a Windows Phone 8.1 app from Visual Studio, the only way to trigger Suspend/Resume event is using the Lifecycle Events in VS. But when you run the app without VS, those methods will trigger as expected. :)