How to Monitor the Event of Application Quit

Hi, I am working on unity 2022.3.11f1 and magic leap SDK 1.5 and magic leap API 2.0.

I am trying to have a call back function that triggered when the user quit the application at home page of magic leap by clicking the "x" next to the application name. but the OnApplicationQuit() function from unity doesn't work in this case. I searched online and found RegisterApplicationQuit and OnQuitEventDelegate maybe helpful. However, after several tries, I didn't figured out how to use these functions.

Are they the correct option to implement the task? If so, may I have some examples/explanation about how to implement it? If not, are there any other options of this task?
Thank you!

Hi @yanghouze , since we are an Android derived platform it is good practice to follow best practices for Android when it comes to the application lifecycle. Waiting for OnApplicationQuit on Android is not recommended but rather you should treat OnPause (or OnApplicationPause(bool) in Unity) as if it will close the application.

You can see here in Android's onDestroy method (which is mapped to Unity's OnApplicationQuit)

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here.

This should be fairly straightforward for a non-networked application as you should be saving / journaling whatever app data you need during pause. If you are doing back-grounded work it can be a little more complex but assuming you are running in the background I would make some co-routine off the main loop and manage the connection that way.

Does that help?

1 Like