Form life cycle
Hi ,
I need someone to explain me the life cycle for Forms or in generally life cycle in codename one.
For test propose I’m using 2 forms created with designer. Both forms have one button to show the other form.
I was expected that every time I call Form.show() , the current form is cleared and the new one is created. From user point of view it looking so but internally I don’t see this behaviour.
I’ve putted 2 Log messages in “beforeForm” and “postForm” but the messages show up only once when I toggle between Forms.
Question : why the log messages are not trigger every time I toggle between Forms?
I got only this output:
[EDT] 0:0:0,0 - Before Main
[EDT] 0:0:0,16 - Post Main
For good practice in CN1 I have the following questions:
- How can I send data between 2 forms?
Let’s consider any data, is ok if I make it available in StateMachine class and then I update it inside created Forms?
For example take a look at “mainForm” from the below source code.
- How “addComponent “ should be used during Form life cycle?
Let’s consider that we add only one custom component in an empty container when Form starts.
Should I check before I add it, if the component already exists from previous Form life cycle?
Is require to clear the container before Form exit?
The custom component is keep it between calls of Form.show() ?
Thanks
Source code for the issue:
public class StateMachine extends StateMachineBase {
static Form mainForm; // refenice to main form to go back
public StateMachine(String resFile) {
super(resFile);
// do not modify, write code in initVars and initialize class members there,
// the constructor might be invoked too late due to race conditions that might occur
}
/**
* this method should be used to initialize variables instead of
* the constructor/class scope to avoid race conditions
*/
protected void initVars(Resources res) {
}
@Override
protected void beforeMain(final Form f){
Log.p("Before Main");
}
@Override
protected void postMain(Form f) {
Log.p("Post Main");
mainForm=f;
}
/*
-----------------------------
CONNECTION ERROR FORM
-----------------------------
*/
void showConnectionErr(String err_reason){
Form err_form = (Form)createContainer(fetchResourceFile(), "ConnectionErr");
findErrLb(err_form).setText(err_reason);
err_form.show();
}
@Override
protected void onConnectionErr_GoHomeBtAction(Component c, ActionEvent event) {
mainForm.showBack(); //<------ show main Form back
}
@Override
protected void onMain_NextFormAction(Component c, ActionEvent event) {
showConnectionErr("bla"); //<------ show Err Form
}
}