新代号 One bare-bones project 开始一个新项目

New Codename One bare-bones project to start a new project

我现在注意到通过 https://start.codenameone.com/

获得的 Java 源代码发生了根本性变化

显然我已经习惯了使用 init()、start()、stop()、destroy() 方法进行编码,我不知道如何处理这些新代码。它记录在某处吗?我在博客里什么都没看到。谢谢

Lifecycle 的 javadoc (https://www.codenameone.com/javadoc/com/codename1/system/Lifecycle.html) 不是很有用。它只是指出:

Optional helper class that implements the Codename One lifecycle methods with reasonable default implementations to help keep sample code smaller.

所以下面的代码只是一个示例,我需要删除,然后手动插入通常的 init()、start()、stop() 和 destroy() 方法?

public class MyDownloader extends Lifecycle {
    @Override
    public void runApp() {
        Form hi = new Form("Hi World", BoxLayout.y());
        Button helloButton = new Button("Hello World");
        hi.add(helloButton);
        helloButton.addActionListener(e -> hello());
        hi.getToolbar().addMaterialCommandToSideMenu("Hello Command",
        FontImage.MATERIAL_CHECK, 4, e -> hello());
        hi.show();
    }

    private void hello() {
        Dialog.show("Hello Codename One", "Welcome to Codename One", "OK", null);
    }

}

此处讨论:https://www.reddit.com/r/cn1/comments/opifd6/a_new_approach_for_the_lifecycle_main_class/

请注意生命周期方法仍然存在,您可以根据需要覆盖它们,但在大多数情况下您并不需要:

When we started Codename One we copied the convention in Java of having a very verbose lifecycle class. This seemed like a good and powerful approach when we just started. The thing about core decisions like that is that you end up accepting them as dogma even when you yourself made them.

That was just stupid and with my recent commit this should be fixed. With this your main class can derive from the new Lifecycle object and you would be able to remove a lot of boilerplate. E.g. this would be the new "Hello World":

public class MyApp extends Lifecycle {
    public void runApp() {
        Form hello = new Form("Hello", BoxLayout.y());
        hello.add(new Label("Hello World"));
        hello.show();
    }
}

That removes a lot of the stuff most of us don't touch in the lifecycle class while still allowing us to customize it if we need to do that.