Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling suspend, resume, and activation in windows 10 UWP

In the windows 8.1 universal apps, the suspend/resume modes were handled using the NavigationHelper.cs ans SuspensionManager.cs classes included in the APP template. These classes doesn't seem to be there in the windows 10 UWP apps. Is there a way by which we can handle the suspend/resume states?

like image 464
Bells Avatar asked Jul 27 '15 08:07

Bells


People also ask

How do I disable UWP app suspension?

Go to Start > Settings > Privacy > scroll down to Background apps > select the app in question & toggle "Off" to turn it off. (Note: Once the app is minimized, it set the app as suspended again.)

How do I resume my UWP process?

To resume the process, right-click on it again, and then choose to resume it from the menu.

Is Microsoft abandoning UWP?

The current trend is that UWP's internal components (WinUI/MSIX) are being expanded to support non-UWP scenarios, so UWP should remain active in the coming years, and we can see how far it goes.

How do I disable UWP in Windows 10?

To do this, press the Start button and go to Settings -> Apps -> Apps and features. In the list of apps, select the app to be uninstalled. Click the Uninstall button. This will only uninstall the UWP app in the current user's profile.


2 Answers

There's an interesting framework being developed by the community (but mostly I think Jerry Nixon, Andy Wigley etc.) called Template10. Template10 has a Bootstrapper class with OnSuspending and OnResuming virtual methods that you can override. I am not sure that there's an exact example of doing suspension/resuming yet with Template10, but the idea seems to be to make App.xaml.cs inherit from this Bootstrapper class so you can easily override the methods I mentioned.

sealed partial class App : Common.BootStrapper
{
    public App()
    {
        InitializeComponent();
        this.SplashFactory = (e) => null;
    }

    public override Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
    {
        // start the user experience
        NavigationService.Navigate(typeof(Views.MainPage), "123");
        return Task.FromResult<object>(null);
    }

    public override Task OnSuspendingAsync(object s, SuspendingEventArgs e)
    {
        // handle suspending
    }

    public override void OnResuming(object s, object e)
    {
        // handle resuming
    }
}
like image 112
Igor Ralic Avatar answered Oct 07 '22 00:10

Igor Ralic


The above solution will only work for people who install Template10. The generic solution is,

paste these lines in the constructor of App.xaml.cs

        this.LeavingBackground += App_LeavingBackground;

        this.Resuming += App_Resuming;

It will look like this

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

        this.Resuming += App_Resuming;
    }

These are the methods, although you can press TAB and they will autogenerate.

    private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
    {

    }

    private void App_Resuming(object sender, object e)
    {

    }

The methods LeavingBackground and the one not mentioned here EnteredBackground are newly added to uwp.

Before these methods we would use resuming and suspending to save and restore ui, but now the recommended place to do that work is here.Also these are the last places to perform work before the app is resumed. So the work on these methods should be small ui or other stuff like remaking values which are stale as a long held method here will affect app startup time while resuming.

Source Windows dev material , Windoes dev material 2

Thanks , and have a good day.

like image 26
hispeed Avatar answered Oct 07 '22 01:10

hispeed