Skip to content

ApplicationController

Steve Hannah edited this page Jun 29, 2021 · 1 revision

ApplicationController

See javadoc

Synopsis

The main application controller. This is the root controller for the entire application. Applications should extend this class and use it as their main application class. Then can then override the actionPerformed() method to handle events of type ApplicationController.StartEvent, ApplicationController.StopEvent, ApplicationController.DestroyEvent, and ApplicationController.InitEvent.

The application controller is ideally suited to act as the main class for a CodenameOne application. It implements all of the lifecycle methods (init(java.lang.Object), Controller.onStartController(), Controller.onStopController(), and destroy(), and dispatches corresponding events that you can handle in your controller.

Example

Typical application structure, replacing the main app class with an ApplicationController.

package com.example.myapp;

import com.codename1.rad.controllers.ApplicationController;
import com.codename1.rad.controllers.ControllerEvent;
import com.codename1.rad.sampler.providers.SampleListProvider;

import static com.codename1.rad.util.NonNull.with;

public class MyApplication extends ApplicationController {
    public void actionPerformed(ControllerEvent evt) {
        with(evt, StartEvent.class, startEvent -> { (1)
            if (!startEvent.isShowingForm()) { (2)
                startEvent.setShowingForm(true); (3)
                new StartPageController(this).show(); (4)
            }
        });
        super.actionPerformed(evt);
    }
}
  1. The NonNull.with() method is a clean way to only handle events of type StartEvent, since the actionPerformed() method will receive all kinds of events.

  2. startEvent.isShowingForm() checks if something has already displayed a form in this start event. E.g. If the app is coming back into the foreground, it will display the "current" form again. In this case, we don’t want to display our start page.

  3. setShowingForm(true) declares to all who might process this event after us, that we are showing a form already, so unless they have a good reason, they should probably not display their own form.

  4. Creates a new form controller, and "shows" it.

Clone this wiki locally