app: [Android] detach previous View when another is created

The Android system can in some cases replace the GioView of our Android
Activity before destroying the previous one. This change makes sure the
previous view is ignored, in particular its destroy event.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-08-29 18:58:53 +02:00
parent b86928ceec
commit 07e1df3676
2 changed files with 45 additions and 12 deletions
+15 -5
View File
@@ -161,6 +161,7 @@ var gioView struct {
setNavigationColor C.jmethodID
setStatusColor C.jmethodID
setFullscreen C.jmethodID
unregister C.jmethodID
}
// ViewEvent is sent whenever the Window's underlying Android view
@@ -315,6 +316,7 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
m.setNavigationColor = getMethodID(env, class, "setNavigationColor", "(II)V")
m.setStatusColor = getMethodID(env, class, "setStatusColor", "(II)V")
m.setFullscreen = getMethodID(env, class, "setFullscreen", "(Z)V")
m.unregister = getMethodID(env, class, "unregister", "()V")
})
view = C.jni_NewGlobalRef(env, view)
wopts := <-mainWindow.out
@@ -325,6 +327,9 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
}
windows[wopts.window] = w
}
if w.view != 0 {
w.detach(env)
}
w.view = view
w.callbacks.SetDriver(w)
handle := C.jlong(view)
@@ -339,11 +344,7 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
//export Java_org_gioui_GioView_onDestroyView
func Java_org_gioui_GioView_onDestroyView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
w := views[handle]
w.callbacks.Event(ViewEvent{View: 0})
w.callbacks.SetDriver(nil)
delete(views, handle)
C.jni_DeleteGlobalRef(env, w.view)
w.view = 0
w.detach(env)
}
//export Java_org_gioui_GioView_onStopView
@@ -442,6 +443,15 @@ func Java_org_gioui_GioView_onWindowInsets(env *C.JNIEnv, class C.jclass, view C
}
}
func (w *window) detach(env *C.JNIEnv) {
callVoidMethod(env, w.view, gioView.unregister)
w.callbacks.Event(ViewEvent{})
w.callbacks.SetDriver(nil)
delete(views, C.jlong(w.view))
C.jni_DeleteGlobalRef(env, w.view)
w.view = 0
}
func (w *window) setVisible() {
width, height := C.ANativeWindow_getWidth(w.win), C.ANativeWindow_getHeight(w.win)
if width == 0 || height == 0 {