Main Window

Overview

Every TotalCross program must have one and only one class that extends totalcross.ui. MainWindow. It is the interface between the VM and the TotalCross program: it receives all events and dispatches them.

Usage

import totalcross.ui.*;
public class MainWindowSample extends MainWindow {
	
	public MainWindowSample() {
		setUIStyle(Settings.MATERIAL_UI);
		setDefaultFont(Font.getFont(Fonts.FONT_DEFAULT_SIZE));
	}
	
	static {
		Settings.applicationId = "MWSP";
		Settings.appVersion = "1.0.1";
		Settings.iosCFBundleIdentifier = "com.totalcross.sample.mainwindowsample";
	}
	
	public void initUI() {
		// add controls here
	}

	public void onExit() {
		// close stuff here
	}
}

The VM first calls the default MainWindow constructor (in this case, MainWindowSample()), and then queues a timer event, which calls the initUI() method inherited from the Container class. You must initialize the entire user interface in the initUI() method.

Initializing the UI in the constructor may hang the application and some operations can only be performed after the initUI(MainWindow) method is reached.

The onExit() method is called when the VM exits under normal circumstances, that is, if the user closes the application or the programmer terminates the application using exit() or exec() or even when the application is interrupted by an exception treated.

In abnormal circumstances where a fatal error occurs by resetting the device, the onExit() method is not called. When this is called, all segments are already dead.

At line 5 we use the setUIStyle() method to assign the style of our application as Settings.MATERIAL_UI and so all components and effects will be in the Google Material Design standard.

To change the font used by all the created controls, you must call the setDefaultFont() method on line 6. In this case, we are setting the default font size that we defined in the Fonts class. To better understand this process, just click here.

Still, inside the constructor, you can define MainWindow title and/or style using setTitle() and setBorderStyle() methods however there is performance loss. To reverse this loss, you can use the super(title, style) method, but this method can not be used with setDefaultFont(). Styles for border can be:

  • Window.NO_BORDER;

  • Window.RECT_BORDER;

  • Window.ROUND_BORDER;

  • Window.TAB_BORDER;

  • Window.TAB_ONLY_BORDER.

But these decisions must be made on the basis of the design of your application. TotalCross recommends that you all follow Material Design.

The Application Lifecycle

The application on TotalCross has a predefined life cycle, handled by defined methods in MainWindow:

On WP8, there is no way to kill an application. To kill it, you must restore it pressing back in the main menu and them openning the application again. Then, press back until finishing the application.

It is also a good practice to save all necessary data and application state so that the when the user goes back to the application it won’t need to do a task again, such as entering data again in a form which was lost when the application was closed by the system. Of course there are situations where the user must retype data, such as login and password and when using bank applications.

Remember that applications for Android, iOS, and Windows Phone 8 shouldn’t have an exit button or an exit option in the menu. The application should be designed to be opened all the time and only the system should close it when needed. Again, there are exceptions, for example when the user doesn’t agree with an eula and the application can’t continue running.

Finally, it is a good idea to map the device back key on Android and WP8 to go back to the last window/menu used. Just remember that on WP8 back is back, you just can use it to go back to the last screen used. On the first screen, back should exit the application. The only exception are games, where back during a game play goes to the pause window.

References

  • To know more details about totalcross.ui.MainWindow, read its JavaDocs.

Last updated