dxUi Page Loading and Switching
This section explains how to switch between pages.
Screen Layers
LVGL divides the screen into three layers from bottom to top: the Main layer, the TOP layer, and the Sys layer. These three layers overlap, and a lower layer is only visible if the layer above it is transparent.
We usually use two of these layers:
- Main Layer: Primarily used for drawing the main page content. Pages are loaded using the
loadMainfunction, and page navigation is achieved by switching pages withloadMain. - TOP Layer: Mainly used for elements that should always be visible on top, such as a status bar showing the time. No matter how the Main layer switches pages, the TOP layer remains visible. The TOP layer can also be used to draw a full page; in that case, other pages can only be shown by hiding the current top page.
Note: To ensure a smooth UI, it's best to hide any elements that are not visible. For example, if a page on the TOP layer is opaque and completely covers the Main layer, you should hide the View on the Main layer. Hidden widgets do not consume rendering resources.
Let's look at an example:
import dxui from "../dxmodules/dxUi.js";
import std from "../dxmodules/dxStd.js";
import logger from "../dxmodules/dxLogger.js";
dxui.init({ orientation: 1 });
const page1 = dxui.View.build("page1", dxui.Utils.LAYER.MAIN);
const button1 = dxui.Button.build("page1button", page1);
button1.setPos(100, 100);
button1.setSize(200, 200);
const buttonLabel1 = dxui.Label.build("page1label", button1);
buttonLabel1.text("Open Page2");
buttonLabel1.setPos(10, 20);
const page2 = dxui.View.build("page2", dxui.Utils.LAYER.MAIN);
const button2 = dxui.Button.build("page2button", page2);
button2.setPos(100, 100);
button2.setSize(200, 200);
button2.bgColor(0xff0000);
const buttonLabel2 = dxui.Label.build("page2label", button2);
buttonLabel2.text("Back Page1");
buttonLabel2.setPos(10, 20);
button1.on(dxui.Utils.EVENT.CLICK, () => {
dxui.loadMain(page2);
});
button2.on(dxui.Utils.EVENT.CLICK, () => {
dxui.loadMain(page1);
});
page1.on(dxui.Utils.ENUM.LV_EVENT_SCREEN_LOADED, () => {
logger.info("page1 loaded");
});
page1.on(dxui.Utils.ENUM.LV_EVENT_SCREEN_UNLOADED, () => {
logger.info("page1 unloaded");
});
page2.on(dxui.Utils.ENUM.LV_EVENT_SCREEN_LOADED, () => {
logger.info("page2 loaded");
});
page2.on(dxui.Utils.ENUM.LV_EVENT_SCREEN_UNLOADED, () => {
logger.info("page2 unloaded");
});
const topview = dxui.View.build("topview", dxui.Utils.LAYER.TOP);
topview.setPos(0, 0);
topview.setSize(200, 30);
topview.bgColor(0xcccccc);
const topviewlabel = dxui.Label.build("topviewlabel", topview);
topviewlabel.text("time");
dxui.loadMain(page1);
std.setInterval(() => {
topviewlabel.text(new Date().toLocaleTimeString());
}, 1000);
std.setInterval(() => {
dxui.handler();
}, 10);
How it Works

Code Analysis
This example code creates two pages, page1 and page2, on the Main layer, and a topview on the TOP layer that is always visible. Clicking the buttons switches between the two pages.
-
Creating Pages:
page1andpage2are bothdxui.Viewobjects withdxui.Utils.LAYER.MAINas their parent.- A button and a label are created on each page for navigation and display.
-
Creating the Top Bar:
- The parent of
topviewisdxui.Utils.LAYER.TOP, so it is displayed above theMAINlayer. - The
TOPlayer remains visible regardless of which page is loaded in theMAINlayer. - We use
setIntervalto update the time ontopviewevery second.
- The parent of
-
Page Switching:
- The key function is
dxui.loadMain(), which loads a new page onto theMAINlayer. When a new page is loaded, the previous page on theMAINlayer is automatically unloaded. - The
clickevent onbutton1callsdxui.loadMain(page2)to displaypage2. - The
clickevent onbutton2callsdxui.loadMain(page1)to displaypage1.
- The key function is
load and unload Events
dxui provides page lifecycle events for loading and unloading, which are useful for initialization and cleanup during page transitions.
dxui.Utils.ENUM.LV_EVENT_SCREEN_LOADED: Triggered when a page is loaded byloadMain.dxui.Utils.ENUM.LV_EVENT_SCREEN_UNLOADED: Triggered when a page is unloaded (i.e., when another page is loaded).
In the example code:
- We listen for these two events on both
page1andpage2. - When switching from
page1topage2, the log will print the following in order:page1 unloadedpage2 loaded
- Conversely, when switching back from
page2topage1, the log will print:page2 unloadedpage1 loaded
Using these events, we can initialize resources (like starting timers or fetching network data) when a page is loaded, and release resources (like destroying timers) when a page is unloaded. This helps to better manage the application's state and memory.