简体   繁体   中英

HoloLens 2 / Unity / App Lifecycle : Handling Resuming

When a HoloLens 2 app is closed by the user it will go into a Suspended state as per the Documention , I would like to perform some actions such as checking the network connectivity when the app is Resumed.

How can this be handled or detected? is there an event that is triggered when the app is Resumed?

In a normal UWP I could do something like:

partial class MainPage
{
   public MainPage()
   {
      InitializeComponent();
      Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
   }

    private void App_Resuming(Object sender, Object e)
    {
        // TODO: Do Something
    }
}

Thanks,

This can be done still. Just need to add this into a Start method.

#if ENABLE_WINMD_SUPPORT
  using Microsoft.Windows.ApplicationModel;  
  using Microsoft.Windows.ApplicationModel.Core;
#endif

Example that can be added into your main GameObject:

    void Start()
    {
       #if ENABLE_WINMD_SUPPORT
          CoreApplication.Resuming += this.OnResuming;  
       #endif
    }

    #if ENABLE_WINMD_SUPPORT
    private void OnResuming(object sender, object args)  
    {  
       //your custom code on resuming
    }  
    #endif

As I couldn't get the Windows CoreApplication.Resuming method to work I have been looking at Unity methods as an alternative.

The one I found that did what I needed was OnApplicationFocus .

On the application being suspended OnApplicationFocus will have the focus parameter set to false. On the application being resumed the focus parameter will be set to true.

I dont beleive that this is triggered on first app load.

using UnityEngine;

public class Example : MonoBehaviour
{
    private void OnApplicationFocus(bool focus)
    {
    
    }
}

Just to be clear my preference is the Windows method if there is a way to get it working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM