Form life cycle in Codename One

498 views
Skip to first unread message

crom_x

unread,
Jul 30, 2014, 6:14:28 PM7/30/14
to codenameone...@googlegroups.com
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
    }
}

Shai Almog

unread,
Jul 31, 2014, 1:39:22 AM7/31/14
to codenameone...@googlegroups.com
Hi,
just store the state of the previous form in the state machine class or a class referenced by that. In the before method you initialize the state of the upcoming form based on the current state of the state machine (as modified by the previous form).

When you add a component to a form that isn't showing (beforeForm stage) this should "just work" since the form is implicitly validated.

When you add a component to a form that is already showing (GUI builder or otherwise) you must indicate that the component was added and trigger a reflow of the UI layout using revalidate. Alternatively you can use one of the animate*Layout methods which is a nicer looking form of revalidate().

CSava Dev

unread,
Jul 31, 2014, 2:43:53 AM7/31/14
to codenameone...@googlegroups.com
Hi , 

I still have one question remaining .

Why "beforeMain" is not called when I  use mainForm.showBack() ?
I don’t see Log.p("Before Main")  to be called.
For more info take a look at code I putted in the previous post.

Thanks.


--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/Tcp0FP2ay14/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/df42043a-574a-44e0-8846-09284a9f2402%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Shai Almog

unread,
Jul 31, 2014, 9:49:36 AM7/31/14
to codenameone...@googlegroups.com
Hi,
show & showBack are methods of Form not of the state machine so if you invoke one of them it doesn't go thru the statemachine/UIBuilder at all.
back() would unwind the stack correctly and return to the previous form as you would expect.

orme...@gmail.com

unread,
May 29, 2015, 2:36:34 AM5/29/15
to codenameone...@googlegroups.com
Hi, Shai,
I'm having the exact same issue as the OP here, the beforeForm() method isn't being hit even though I'm using "back" and "show" form-level commands I attached in the GUI builder.  Is this not the proper way to do things?

בתאריך יום חמישי, 31 ביולי 2014 בשעה 16:49:36 UTC+3, מאת Shai Almog:

Shai Almog

unread,
May 29, 2015, 11:49:05 AM5/29/15
to codenameone...@googlegroups.com, orme...@gmail.com, orme...@gmail.com
Hi,
please don't reopen an old thread. The issue above was answered.
Reply all
Reply to author
Forward
0 new messages