代号 One stop() 方法:我们为什么不保留 Dialog?

Codename One stop() method: why don't we keep Dialog?

默认代号一方法是:

public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

真的有必要配置一个Dialog吗?为什么? 去掉Dialog相关代码的优缺点?

start() 被调用来恢复应用程序。因此,如果它被最小化,stop() 将被调用并处理对话框。假设它不这样做... start() 将再次调用并再次显示对话框。

对话框的show()方法正在阻塞。因此它将停止当前的回调并有效地破坏整个恢复过程。

作为替代方案,我们过去曾尝试检查这是否是关于恢复和使用 showModless() 的对话框,但这与某些端口代码有关,该代码也会在当前 [=] 上调用 show() 16=]。唯一可行的解​​决方案是将对话框实例保存为特例并进行处理。然后 re-show 它在 start().

中带有 callSerially()

由于某种我不知道的原因,我之前的尝试失败了。终于找到了完美解决问题的代码。我在 Android 10 和 iOS 13 的模拟器中成功测试了它。它基于 Shai 的响应 .

public class MyApplication {

    private Form current;
    private Resources theme;
    private Dialog toBeRestored;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            if (toBeRestored != null) {
                CN.callSerially(() -> toBeRestored.show());
            }
            return;
        }
        Form hi = new Form("Persistent Dialog", BoxLayout.y());
        Button button = new Button("Open Dialog");
        hi.add(button);
        hi.show();

        button.addActionListener(l -> {
            Container dialogBody = new Container(BoxLayout.y());
            dialogBody.add(new SpanLabel("Dialog Body"));
            Dialog d = new Dialog(new BorderLayout()){
                @Override
                public void dispose() {
                    toBeRestored = this;
                    super.dispose();
                };
                
                @Override
                public void show() {
                    toBeRestored = null;
                    super.show();
                };
            };
            d.setDisposeWhenPointerOutOfBounds(false);
            d.add(BorderLayout.CENTER, dialogBody);
            Button okBtn = new Button("OK");
            d.add(BorderLayout.SOUTH, FlowLayout.encloseCenter(okBtn));
            
            okBtn.addActionListener(ll -> {
                d.dispose();
            });
            
            d.show();
        });
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}