diff --git a/core/src/components/tab-bar/tab-bar.tsx b/core/src/components/tab-bar/tab-bar.tsx index d17bb0ba145..d8a03cdb71c 100644 --- a/core/src/components/tab-bar/tab-bar.tsx +++ b/core/src/components/tab-bar/tab-bar.tsx @@ -22,6 +22,7 @@ import type { TabBarChangedEventDetail } from './tab-bar-interface'; }) export class TabBar implements ComponentInterface { private keyboardCtrl: KeyboardController | null = null; + private keyboardCtrlPromise: Promise | null = null; private didLoad = false; @Element() el!: HTMLElement; @@ -88,7 +89,7 @@ export class TabBar implements ComponentInterface { } async connectedCallback() { - this.keyboardCtrl = await createKeyboardController(async (keyboardOpen, waitForResize) => { + const promise = createKeyboardController(async (keyboardOpen, waitForResize) => { /** * If the keyboard is hiding, then we need to wait * for the webview to resize. Otherwise, the tab bar @@ -100,11 +101,32 @@ export class TabBar implements ComponentInterface { this.keyboardVisible = keyboardOpen; // trigger re-render by updating state }); + this.keyboardCtrlPromise = promise; + + const keyboardCtrl = await promise; + + /** + * Only assign if this is still the current promise. + * Otherwise, a new connectedCallback has started or + * disconnectedCallback was called, so destroy this instance. + */ + if (this.keyboardCtrlPromise === promise) { + this.keyboardCtrl = keyboardCtrl; + this.keyboardCtrlPromise = null; + } else { + keyboardCtrl.destroy(); + } } disconnectedCallback() { + if (this.keyboardCtrlPromise) { + this.keyboardCtrlPromise.then((ctrl) => ctrl.destroy()); + this.keyboardCtrlPromise = null; + } + if (this.keyboardCtrl) { this.keyboardCtrl.destroy(); + this.keyboardCtrl = null; } }