I have now noticed a radical change in the source Java code obtained via https://start.codenameone.com/
Obviously I'm used to coding with init(), start(), stop(), destroy() methods, I don't know how to handle this new code. Is it documented somewhere? I didn't see anything in the blog. Thank you
Lifecycle's javadoc (https://www.codenameone.com/javadoc/com/codename1/system/Lifecycle.html) is not very helpful. It just states:
Optional helper class that implements the Codename One lifecycle methods with reasonable default implementations to help keep sample code smaller.
So the following code is just an example that I need to remove, and then manually insert the usual init(), start(), stop() and destroy() methods?
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);
}
}
This is discussed here: https://www.reddit.com/r/cn1/comments/opifd6/a_new_approach_for_the_lifecycle_main_class/
Notice the lifecycle methods still exist and you can override them if you want but for the most part you don't really need to:
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.