# BabylonNative - Is it possible to restart an Android activity?

**URL:** <https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542>\
**Category:** Questions\
**Created:** [September 3, 2021, 10:30am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542 "2021-09-03T10:30:28Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 7, 2021, 10:01am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/21 "2021-09-07T10:01:24Z")

</div>

Indeed `onDestroy` is called, but the question is how to properly stop XR?

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 7, 2021, 10:05am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/22 "2021-09-07T10:05:29Z")

</div>

something like `mView.eval("exitAR();", "ActivityScript");`  
where exitAR is a JS function defined in experience.js

---

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 7, 2021, 10:11am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/23 "2021-09-07T10:11:36Z")

</div>

Ohh interesting, I was trying to stop XR in the native C ++ part and not in the JS part … that’s probably what I was doing wrong …  
I’m trying this this afternoon

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 7, 2021, 10:13am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/24 "2021-09-07T10:13:12Z")

</div>

I’m trying to make it work on my side. destruction is async, might need some await or something to make it work.

my function is :

```auto

function exitAR()
{
    if (currentXR) {
        console.log("Exiting XR");
        currentXR.baseExperience.exitXRAsync();
        currentXR = null;
    }
}

```

and currentXR is assigned with xr just after `enterXRAsync` call

---

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 7, 2021, 11:45am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/25 "2021-09-07T11:45:35Z")

</div>

😭 I still have an issue when g\_graphics tried to be released (forcing `g_graphics.reset();` when activity is destroying or when si restarting …)…

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 7, 2021, 1:22pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/26 "2021-09-07T13:22:11Z")

</div>

I’m still investigating with another idea…

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 8, 2021, 1:46pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/27 "2021-09-08T13:46:44Z")

</div>

It’s an opengl context switching issue. At some point, a (new?) context is bound for the thread but I find no call to make it current. This newly bound context is not the expected one. Still searching …

---

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 8, 2021, 2:27pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/28 "2021-09-08T14:27:02Z")

</div>

Don’t hesitate to let me know if I can help you somewhere …

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 9, 2021, 9:36am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/29 "2021-09-09T09:36:34Z")

</div>

I’m still debugging today. At this stage, I didn’t figure out what is changing the GL context on the BN side. My guess is with Surface/View life cycle, the system destroys something that change the GL Context. I tried wo unbind the current GL context on the Java side in the `surfaceDestroyed` callback without much luck.  
Today, I’ll try to put a breakpoint in eglMakeCurrent to see if anything calls it. And I’ll also try with RenderDoc to see if I get better logs.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 10, 2021, 8:38am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/30 "2021-09-10T08:38:10Z")

</div>

No stacktrace with lldb/AndroidStudio. RenderDoc doesn’t hook on the app (might be my configuration in this case).  
I still believe the Android system does something when XR view is destroyed. I’ll try now to do a minimal repro wihtin XR: keep view management but do not re-create surface in XR.cpp

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 10, 2021, 9:08am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/31 "2021-09-10T09:08:14Z")

</div>

I removed `xrSurfaceView` from BabylonView.java. No view = no surface for XR = no rendering and there is still a context issue. So, it’s might not be related to the surface, I leasst not the way I imagined it. Next on the list is XR resources initialization. Now I’ll disable nativeXR code until it works !

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 10, 2021, 1:08pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/32 "2021-09-10T13:08:48Z")

</div>

I’m removing more and more code. Crash still occurs with same wrong GL context.  
Surface is now never changed. No rendering code in XR.cpp and less rendering code in NativeXR.cpp  
I’m starting to wonder if it might be related to ARCore.  
@Eric_Beets Can you please try to use a more recent version ? Changing the versison in the following lines should do the trick :

> <https://github.com/BabylonJS/BabylonNative/blob/a89ba66567c4ceedaa2e3dc867da4646b99f161a/Apps/Playground/Android/app/build.gradle#L75>

---

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 10, 2021, 3:58pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/33 "2021-09-10T15:58:25Z")

</div>

I think that I have already tried it, I just re-test, and still a crash.  
But I also have the feeling it come from the ARCore, some resources are not released/close or something like that …

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 10, 2021, 4:03pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/34 "2021-09-10T16:03:17Z")

</div>

yes, I’ve update the issue on GitHub:

> <https://github.com/BabylonJS/BabylonNative/issues/878>
>
> Follow up https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-…an-android-activity/23542/24
> 
> \*\*Describe the bug\*\*
> Crash second time launching the playground activity when XR was active the first time.
> 
> \*\*To Reproduce\*\*
> Steps to reproduce the behavior:
> 1. Clone this blanch https://github.com/EricBeetsOfficial-Opuscope/BabylonNative/tree/test/Android\_lifecycle\_activities
> 2. Build & run for Android
> 3. tap button 'Launch Playground'
> 4. wait for XR to be active
> 5. Get back to main activity
> 6. tap again 'Launch Playground'
> 
> \*\*Expected behavior\*\*
> No crash 
> 
> \*\*Other\*\*
> - Android Only. ViewController life management is different on iOS.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 13, 2021, 12:52pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/35 "2021-09-13T12:52:47Z")

</div>

Doing a bit of progress: Binding opengl context only and not the surface make it not crash in later use of the context.

```auto
EGLDisplay previousDisplay{ eglGetDisplay(EGL_DEFAULT_DISPLAY) };
EGLSurface previousDrawSurface{ eglGetCurrentSurface(EGL_DRAW) };
EGLSurface previousReadSurface{ eglGetCurrentSurface(EGL_READ) };
EGLContext previousContext{ eglGetCurrentContext() };
eglMakeCurrent(eglGetDisplay(EGL_DEFAULT_DISPLAY), EGL_NO_SURFACE, EGL_NO_SURFACE, previousContext);
ArSession_update(xrContext->Session, xrContext->Frame);
eglMakeCurrent(previousDisplay, previousDrawSurface, previousReadSurface, previousContext);

```

Now, there are a bunch of issues with engine destruction/re-initialization…

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 13, 2021, 12:59pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/36 "2021-09-13T12:59:59Z")

</div>

@Eric_Beets

I’ve opened a PR on your branch, can you test it please?

---

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 13, 2021, 4:48pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/37 "2021-09-13T16:48:17Z")

</div>

I just tested the, it crashes to renderer\_gl.h:1338

 ![Screenshot 2021-09-13 at 18.45.49](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/9/79ea246ae718b2d6cf49564737234925e505c933.png)

With the following stack:

```auto
2021-09-13 18:46:26.662 21920-22035/com.android.babylonnative.playground I/BabylonNative: BJS - [18:46:26]: FPS: Infinity
2021-09-13 18:46:47.522 21920-22095/com.android.babylonnative.playground E/native: E0913 18:46:47.522607 22095 camera_image_stream.cc:271] FAILED_PRECONDITION: Failed to extract the native metadata, status=DEADLINE_EXCEEDED: Timed out waiting for metadata.
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/metadata_cache.cc:53
    third_party/arcore/ar/camera/android/metadata_extraction.cc:54
    
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/camera_image_stream.cc:205
2021-09-13 18:46:57.905 21920-22095/com.android.babylonnative.playground E/native: E0913 18:46:57.905314 22095 camera_image_stream.cc:271] FAILED_PRECONDITION: Failed to extract the native metadata, status=DEADLINE_EXCEEDED: Timed out waiting for metadata.
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/metadata_cache.cc:53
    third_party/arcore/ar/camera/android/metadata_extraction.cc:54
    
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/camera_image_stream.cc:205
2021-09-13 18:46:57.906 21920-22095/com.android.babylonnative.playground E/native: E0913 18:46:57.906103 22095 camera_image_stream.cc:232] Camera Image Stream failed to dequeue Image from ImageReader. status=UNAVAILABLE: AImageReader_acquireLatestImage [type.googleapis.com/util.ErrorSpacePayload='ImageReaderStatusErrorSpaceClass::AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE']
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/imagereader_status_error_space.cc:112
    third_party/arcore/ar/camera/android/imagereader_ndk_wrapper.cc:241
     acquired_image_count=3
2021-09-13 18:47:14.422 21920-21941/com.android.babylonnative.playground E/ACameraDevice: Frame number out of order. Expect 5 but get 10
2021-09-13 18:47:21.402 21920-22095/com.android.babylonnative.playground E/native: E0913 18:47:21.402012 22095 camera_image_stream.cc:271] FAILED_PRECONDITION: Failed to extract the native metadata, status=DEADLINE_EXCEEDED: Timed out waiting for metadata.
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/metadata_cache.cc:53
    third_party/arcore/ar/camera/android/metadata_extraction.cc:54

```

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 14, 2021, 11:55am UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/38 "2021-09-14T11:55:48Z")

</div>

I moved engine shutdown in main activity. I get no more crash with multiple back and forth between main & playground activities.  
PR is in sync with my latest changes.

---

<div class="post-metadata">

**Author:** ![Eric\_Beets](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/eric_beets/32/15568_2.png) [@Eric\_Beets](https://forum.babylonjs.com/u/Eric_Beets)\
**Post date:** [September 14, 2021, 2:30pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/39 "2021-09-14T14:30:23Z")

</div>

🤔 I just tried it on S8+ and S9+, and it still crashes …  
I tested it on a Xiaomi MiA3, the app does not crash, but when I restart the AR activity, the default box is not in the scene.  
Is there something particular on Samsung devices ?

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 14, 2021, 2:42pm UTC](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542/40 "2021-09-14T14:42:06Z")

</div>

yes, we faced some issues with Samsung. Like this one : [AR view broken on Samsung SM-T820 · Issue #788 · BabylonJS/BabylonNative · GitHub](https://github.com/BabylonJS/BabylonNative/issues/788)  
Let me take a look for the default box.

[Previous page](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542.md?page=1)

[Next page](https://forum.babylonjs.com/t/babylonnative-is-it-possible-to-restart-an-android-activity/23542.md?page=3)
