diff --git a/CodenameOne/src/com/codename1/ui/Display.java b/CodenameOne/src/com/codename1/ui/Display.java index 34bf4c4034..e9e08943f5 100644 --- a/CodenameOne/src/com/codename1/ui/Display.java +++ b/CodenameOne/src/com/codename1/ui/Display.java @@ -1495,8 +1495,17 @@ void setCurrent(final Form newForm, boolean reverse) { if (edt == null) { throw new IllegalStateException("Initialize must be invoked before setCurrent!"); } - Form current = impl.getCurrentForm(); + if (!isEdt()) { + // when not running callSerially executes synchronously and would recurse here forever (#4811) + if (!codenameOneRunning) { + throw new IllegalStateException("Display.setCurrent must be invoked after Codename One has started running. Call it from start() or via callSerially."); + } + callSerially(new RunnableWrapper(newForm, null, reverse)); + return; + } + + Form current = impl.getCurrentForm(); if (autoFoldVKBOnFormSwitch && !(newForm instanceof Dialog)) { setShowVirtualKeyboard(false); @@ -1528,11 +1537,6 @@ void setCurrent(final Form newForm, boolean reverse) { } } - if (!isEdt()) { - callSerially(new RunnableWrapper(newForm, null, reverse)); - return; - } - if (current != null) { if (current.isInitialized()) { current.deinitializeImpl();