Home / Codename One     frequal.com

Timer Won't Restart When Reshowing a Form -- How to Fix

Summary

In the postMain() method, pass the Form parameter to Timer.schedule(). Getting the Form via other mechanisms doesn't work.

Details

postMain() is the auto-generated helper method called immediately after your Main form has been displayed. If you want to start a timer when a different form is shown, override the desired postXXX method.

In postMain(), if you have created a timer (using new UITimer(), for example), you'll then want to start it using timer.schedule(). The third parameter is a Form. Be sure to pass in the one passed to postMain() as the first parameter.

  @Override
  protected void postMain(Form form) {
    [...]
    timer.schedule(1000, true, form); // Works
If you try to use Display.getInstance().getCurrent(), you'll find your timer doesn't start sometimes since Display still has the old form. (This is at least an undocumented behavior, at worst a bug in CN1.)
  @Override
  protected void postMain(Form form) {
    [...]
    timer.schedule(1000, true, Display.getInstance().getCurrent()); // AVOID -- doesn't work

Last modified on 10 Oct 2015 by AO

Copyright © 2024 Andrew Oliver