I had a question about creating a SurfaceView and then getting ANativeWindow from it.
- Is this right to do
mSurfaceView = new SurfaceView(this);
in:
The reason for the request: as I understand it, SurfaceView will be destroyed when we lose focus (something else covers the entire screen), so we will need to recreate it every time we get focus (onStart () is executed), or SurfaceView remains inactive and reusable?
Reason for request: onSurfaceChanged seems to always be called after onSurfaceCreated
, so we have a choice when to create our own window. On the one hand, it seems logical to do this in onSurfaceCreated
, but two jobject surface
seem to refer to different objects! (As verified by creating a weak global reference to the surface in onSurfaceCreated and checking it for both NULL and the surface in onSurfaceChanged, see the code below)
onSurfaceCreated_native(JNIEnv env, ... ,jobject surface) {
myWeakObjectGlobal = env->NewWeakGlobalRef(surface);
}
onSurfaceChanged_native(JNIEnv env, ... ,jobject surface) {
if (env->IsSameObject(surface, myWeakObjectGlobal)) { LOGW("onSurfaceChanged_native: new surface is SAME as old surface"); } else { LOGW("onSurfaceChanged_native: new surface is DIFFERENT as old surface"); } if (env->IsSameObject(NULL, myWeakObjectGlobal)) { LOGW(" furthermore, old surface is NULL"); } else { LOGW(" furthermore, old surface is NOT null"); }
}
Therefore, if two different surface objects are sent to onSurfaceCreated and onSurfaceChanged, we want to use the most recent ones and not hang on an obsolete reference to the surface and, therefore, do ANativeWindow_from_Surface in onSurfaceChanged.
I would really appreciate if anyone could cover this issue for me.