BabylonNative - Is it possible to restart an Android activity?

Hi there,

Related to this first question : (cc @Cedric)

I am able to reproduce the issue with Android Playground App by adding a MainActivity (modifying the AndroidManifest.xml) which call the PlaygroundActivity (by a clicked button)

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

        final Button button1 = findViewById(;
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, PlaygroundActivity.class);

MainActivity is running → click on the button → PlaygroundActivity is running → press back button → MainActivity is running → click on the button → app crash with the following callstack:

2021-09-03 12:24:54.512 5711-6208/ E/native: E0903 12:24:54.512093    6208] Failed to compute ML depth: INVALID_ARGUMENT: Not enough camera landmark priors to estimate scale and shift.
    === Source Location Trace: ===

Does anyone have an idea?
Thanks for the great BabylonNative job !!


Let me try to repro :slight_smile:

I forgot to mention that I was using the enable AR mode (in the experience.js file)…

1 Like

@Eric_Beets Can you please push your BabylonNative test branch? I’m having difficulties doing a repro

You can find a test branch here:

(Im building on macOS…)

Let me know if you need something, thank you for your help,


1 Like

I can repro…

It’s a good and bad news …
Did I make something wrong ?

I don’t think so. My guess is something should have been uninitialized (GL context?) from a window in Xr component and is obviously not. I need to find what :slight_smile:

It looks like it might not be possible to change the native window with XR while it’s running. I’ll get the confirmation beginning of next week.
I’ve got 1 question for you. Playground activity is not destroyed when getting back to mainActivity (finalize is not called). I guess Android may destroy it later? I think, destroying the playgroundActivity(and its view) when getting back to main will fix the issue.

I have already tried to force the Android views/activity destruction but the GC is not always agree with this idea :stuck_out_tongue: .
I going to make other additional tests to force the finalize .

We keep in touch if we have more info !
Thank you for your help !

1 Like

I got confirmation window should not change when XR is working. To prevent that, playgroundActivity must be finalized or its view must be destroyed when getting back to mainActivity.

1 Like

I definitely can’t find a clean way to stop/release the AR camera (the app crashes into arsession) … maybe I have to find to keep the activity alive (like on iOS …), what do you think ?

Do you know if it exists a way to stop the NativeXr (maybe the FrameTask) ?

adding @ryantrem

1 Like

Hi there,
I have tried working on my problem again for last few days without finding any solutions.
Not being able to close the ArCamera in an Activity and restart it afterwards is a very blocking issue for us.
I would really appreciate any solutions, advices on it (I will obviously be delighted to contribute if I found solution…)

Thanks in advance,

The only thing I see for now if to try to detect when getting back to main activity and destroy engine.
This means it will take some time when going from mainActivity to the playgroundActivity.
Are you planning to do the same with iOS?

The android lifecycle seems to be hard to “override” (the finalize function is called by the GC and it cannot be forced : “In general it’s best not to rely on finalize() to do any cleaning up etc.ref), so, I don’t reaaly how to destroy the engine.
I already have the same thing for iOS: the lifecycle is more compliant (there is no automatic GC and the UIViewController are not destroyed)

Another option I can try is to detach the content view from the activity, store it as a static and re-set it when the activity is back.

I have also tried that (but I hope I did it wrong), you are talking about SurfaceView
(primarySurfaceView, xrSurfaceView) ?
Actually, it seems to work when Xr mode is disabled, it would seem there is a problem when the camera is enabled and the camera is attached to the SurfaceView (linked to the g_graphics which is linked to the ANativeWindow which is linked to the xrSurfaceView by the SurfaceHolder … hoping my stack is correct …).
I will give it another try on that (keeping this SurfaceView) hoping the finalize function won’t never called :stuck_out_tongue:

engine supports having a new view at any time. but XR, by design doesn’t. And at some point, it tries to make current an opengl context to a view that doesn’t exist anymore. hence the crash.

Which makes me thinks: activity.onDestroy is called when getting back to mainActivity. Why evaluating a script that stops XR when it happens?