Removing added views

The default LuaLoader class from the Android template includes a show() method that creates a webView and adds to the Corona activity, so I’m building my own plugin based on this same approach to get a camera surface view onto the activity. e.g:

public class start implements com.naef.jnlua.NamedJavaFunction { // This reports a class name back to Lua during the initiation phase. @Override public String getName() { return "start"; } // This is what actually gets invoked by the Lua call @Override public int invoke(final LuaState luaState) { if(shared.status != "running") { shared.status = "running"; CoronaActivity activity = CoronaEnvironment.getCoronaActivity(); if (activity == null) { return 0; } activity.runOnUiThread(new Runnable() { @Override public void run() { CoronaActivity activity = CoronaEnvironment.getCoronaActivity(); if (activity == null) { return; } cameraPreview mPreview = new cameraPreview(activity); activity.getOverlayView().addView(mPreview); } }); } return 1; } }

(obviously cameraPreview is another class)

Seems to work perfectly, but I can’t find how to remove the surface again? Or potentially better yet, to clear the OverlayView completely. Any pointers please?

Aha, cracked it.

For anybody else who stumbles on to this, moving mPreview into a shared class:

public class shared { public static cameraPreview mPreview; }

And changing the above to this:

shared.mPreview = new cameraPreview(activity); activity.getOverlayView().addView(shared.mPreview);

Means that you can later kill it again, like this:

activity.getOverlayView().removeView(shared.mPreview);

Simples =).

Aha, cracked it.

For anybody else who stumbles on to this, moving mPreview into a shared class:

public class shared { public static cameraPreview mPreview; }

And changing the above to this:

shared.mPreview = new cameraPreview(activity); activity.getOverlayView().addView(shared.mPreview);

Means that you can later kill it again, like this:

activity.getOverlayView().removeView(shared.mPreview);

Simples =).