Codenameone Codename One stop()方法:为什么不';我们不能保持对话吗?

Codenameone Codename One stop()方法:为什么不';我们不能保持对话吗?,codenameone,Codenameone,默认的代码名方法是: public void stop() { current = getCurrentForm(); if (current instanceof Dialog) { ((Dialog) current).dispose(); current = getCurrentForm(); } } 真的有必要处理一个对话框吗?为什么? 消除对话框相关代码的优缺点?start()被调

默认的代码名方法是:

public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }
真的有必要处理一个对话框吗?为什么? 消除对话框相关代码的优缺点?

start()
被调用以恢复应用程序。因此,如果最小化,将调用
stop()
,并处理该对话框。让我们假设它不这样做<将再次调用代码>开始(),并再次显示对话框

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


作为一种替代方法,我们过去曾尝试检查这是否是一个关于还原和使用
showModless()
的对话框,但这与某些端口代码有关,这些端口代码也在当前
表单上调用
show()
。唯一可行的解决方案是将对话框实例保存为特殊情况并进行处置。然后在
start()

中使用
callSerially()
重新显示它,因为我不知道的原因,我以前的尝试失败了。我终于找到了完美解决问题的代码。我在Android 10和iOS 13的模拟器上成功地测试了它。这是基于Shai的反应


不幸的是,«[…]将对话框实例保存为特殊情况并进行处理。然后在start()»中使用callSerialy()重新显示它,它只在模拟器和Android上工作,但在iPhone上不工作。在显示当前表单后,我甚至尝试使用UITimer再次显示该对话框,但它在iOS上不起作用。iOS是我们有此功能的主要原因之一。它的回调逻辑确实更难。尝试使用
showModless()
重新显示它,而不是
show()
。在
callserialy()
中。它不工作。问题是,在iOS上,如果在
stop()
内,我将对话框保存在
currentDialog
变量中,然后在
start()时调用
dispose
code被调用
currentDialog
变量已变为
null
,原因我不知道。我认为如果不同时将currentForm变量设为null,我们无法将currentDialog变量设为null。有可能您没有为当前表单赋值,因此没有执行正确的代码块。我用一个完整的工作测试用例添加了答案。在这段代码中,每次显示对话框时(即使没有最小化),它都会被还原。您应该在
stop()
方法中设置该对话框
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() {
    }

}