Step 3: Events
The framework calls main on every roundtrip — on the initial start and again after each user interaction. As soon as an app reacts to input, main has to tell those calls apart. That job falls to the lifecycle checks, and main becomes a dispatcher:
CLASS zcl_app_walkthrough DEFINITION PUBLIC.
PUBLIC SECTION.
INTERFACES z2ui5_if_app.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_app_walkthrough IMPLEMENTATION.
METHOD z2ui5_if_app~main.
IF client->check_on_navigated( ).
DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
)->ele( n = `View` ns = `mvc`
)->a( n = `xmlns` v = `sap.m`
)->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`
)->ele( `Shell`
)->ele( `Page`
)->a( n = `title` v = `Walkthrough - Step 3`
)->tag( `Text`
)->a( n = `text` v = `Hello World`
)->tag( `Button`
)->a( n = `text` v = `Say Hello`
)->a( n = `press` v = client->_event( `SAY_HELLO` ) ).
client->view_display( view->stringify( ) ).
ELSEIF client->check_on_event( `SAY_HELLO` ).
client->message_toast_display( `Hello from abap2UI5!` ).
ENDIF.
ENDMETHOD.
ENDCLASS.You should see
The page from Step 2 with a Say Hello button under the text. Press it: a small message, Hello from abap2UI5!, appears at the bottom and fades.
The Lifecycle
main ran twice to get you there, once per roundtrip:
| Roundtrip | What the browser sent | Which branch of main ran | What came back |
|---|---|---|---|
| 1 | the start of the app | check_on_navigated( ) | the view |
| 2 | the event SAY_HELLO | check_on_event( `SAY_HELLO` ) | the toast |
check_on_navigated( )is true when the app has to draw its screen — on the first start, and again whenever the user navigates back to it later. This branch displays the view.check_on_event( )is true when the user triggered an event. The argument names which one.client->_event( `SAY_HELLO` )wires the button: it returns the press handler that sends the event — with the name you chose — back tomain.
These two checks are all this tutorial needs. A third one, check_on_init( ), is true only on the very first call and is the place for one-time setup; when you use it, it comes first in the IF/ELSEIF chain. The full picture is on the Life Cycle page.
Next: data leaves the browser and reaches your class — without a single line of transfer code.
Working Samples
Complete apps from the sample catalog that use what this page describes. Each is a single class in abap2UI5/samples unless its row names another of the three sample repositories — pull that repository with abapGit and start the class with ?app_start=<class>.
| Sample | Class |
|---|---|
| Basics III — Lifecycle: Init, Event, Navigated | Z2UI5_CL_SMP_APP_495 |
| Basics IV — Events, Views and Roundtrips | Z2UI5_CL_SMP_APP_004 |
