# Application lifecycle management

**URL:** <https://forum.magicleap.cloud/t/application-lifecycle-management/5343>\
**Category:** Native Development\
**Created:** [March 14, 2025, 1:31pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343 "2025-03-14T13:31:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![emaschino](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@emaschino](https://forum.magicleap.cloud/u/emaschino)\
**Post date:** [March 14, 2025, 1:31pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/1 "2025-03-14T13:31:17Z")

</div>

Hi,

When inside my app, pressing the Home button on the ML2 controller makes the app go through the Android APP\_CMD\_PAUSE, APP\_CMD\_TERM\_WINDOW and APP\_CMD\_STOP status, as expected. Back on the Home Menu, my app is still shown as running with the “blue LED” at the right of its Home Menu item. Clicking again on my app Home Menu item bring it back, going through the Android APP\_CMD\_RESUME and APP\_CMD\_INIT\_WINDOW status. So far, so good.

Now, when on the Home Menu, if I opt to close my app by clicking on its Home Menu item X button, it never goes through the APP\_CMD\_DESTROY status. In fact, it seems there’s no difference between being stopped and closed from the app point of view. So, what event is triggered by ML2 OS when an app is closed by clicking on its Home Menu item X button? Or which event should I register to be notified that my app has been closed?

As a side note, from within my app, if I press the Home button on the ML2 controller and immediately close the app by pressing the X button Home Menu, it doesn’t even have the time to go through the APP\_CMD\_PAUSE, APP\_CMD\_TERM\_WINDOW and APP\_CMD\_STOP status, as if the X button was killing the app without giving me the opportunity to cleanly manage its lifecycle.

Is there any documentation on how ML2 OS manages application lifecycle?

Thanks.

---

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [March 14, 2025, 3:18pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/2 "2025-03-14T15:18:20Z")

</div>

@emaschino Are you testing a C++ app or a Unity App? When I test a standard android app , the onDestroy() function gets called.

```auto
    @Override
    protected void onDestroy() {
        Log.i("test", "onDestroy");
        super.onDestroy();
    }

```

* * *

Magic Leap 2 is expected to have the same lifecycle as an Android 10 device.

> **[The activity lifecycle  |  App architecture  |  Android...](https://developer.android.com/guide/components/activities/activity-lifecycle#alc)**
>
> An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user...

> **[Activity  |  API reference  |  Android Developers](https://developer.android.com/reference/android/app/Activity#onDestroy())**

What version of the OS are you on?

---

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [March 14, 2025, 3:26pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/3 "2025-03-14T15:26:49Z")

</div>

Note, similar to android 10 swiping an app away from the recent tasks list may not trigger the onDestroy() callback. Instead of calling onDestroy(), the OS may simply kill the app’s process immediately.

---

<div class="post-metadata">

**Author:** ![emaschino](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@emaschino](https://forum.magicleap.cloud/u/emaschino)\
**Post date:** [March 14, 2025, 3:54pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/4 "2025-03-14T15:54:06Z")

</div>

I’m testing a C++ app with a template similar to the hello\_xr sample from Khronos Group ([OpenXR-SDK-Source/src/tests/hello\_xr/main.cpp at main · KhronosGroup/OpenXR-SDK-Source · GitHub](https://github.com/KhronosGroup/OpenXR-SDK-Source/blob/main/src/tests/hello_xr/main.cpp)) with latest ML2 OS 1.12.0.

---

<div class="post-metadata">

**Author:** ![emaschino](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@emaschino](https://forum.magicleap.cloud/u/emaschino)\
**Post date:** [March 14, 2025, 3:57pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/5 "2025-03-14T15:57:04Z")

</div>

OK, so if the Close button on Home Menu acts as swiping and indeed kill the apps, what would be the suggested approach to gracefully close an app on ML2? Adding a “close” feature in the app GUI to manage the app shutdown?

---

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [March 14, 2025, 4:12pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/6 "2025-03-14T16:12:27Z")

</div>

Based on Android’s docs, it looks like overall, the best practice is to let Android handle app termination and ensure that your app’s state and resources are managed during the normal lifecycle events.

---

<div class="post-metadata">

**Author:** ![emaschino](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@emaschino](https://forum.magicleap.cloud/u/emaschino)\
**Post date:** [March 14, 2025, 4:37pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/7 "2025-03-14T16:37:14Z")

</div>

Thanks @kbabilinski. In my situation, it turns out that my C++ app is also an OpenXR app, so I had the ML2 controller menu button bound to a Quit action. This way, the OpenXR session is correctly closed and my app now reaches the APP\_CMD\_DESTROY status.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/magicleap1/original/1X/bd9b737a4d45ea243aa7eb177d60a773b806899a.svg) [@system](https://forum.magicleap.cloud/u/system)\
**Post date:** [March 29, 2025, 4:37pm UTC](https://forum.magicleap.cloud/t/application-lifecycle-management/5343/8 "2025-03-29T16:37:26Z")

</div>

Closed on March 29, 2025, 4:37pm UTC

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
