You will find many blogs and wikis on SCN about the creation of your own WebUI component... this article will show you how to integrate it further into the UI framework in order to display a data loss popup each time a user gets out of your component without saving.
Step 1: create method ON_BEFORE_WA_CONTENT_CHANGE
This method will be called to decide wether or not the data loss popup should be displayed when the user is exiting a component. A good and very simple coding example can be found in main window of standard component BP_HIER (Busines Partner hierarchy). So you might want to copy method CL_BP_HIER_MAINWINDOW_IMPL~ON_BEFORE_WA_CONTENT_CHANGE to your own component main window and adapt it in order to:
1) Check if save is needed
Sample coding (in the following lines we assume that lr_entity is the root entity of your Z component):
lr_tx = lr_entity->get_transaction( ).
lv_save_needed = lr_tx->check_save_needed( ).
2) Delegate to dataloss handler in case the popup should be displayed
data_loss_handler->set_save_handler( me ).
data_loss_handler->set_revert_handler( me ).
data_loss_handler->set_cancel_handler( me ).
3) Eventually release lock if nothing to save
IF lr_entity->is_locked( ) EQ abap_true.
lr_tx->revert( iv_suppress_buffer_sync = abap_true ).
Remark: pay attention to ON_BEFORE_WA_CONTENT_CHANGE definition... This method should be marked as event handler for CL_BSP_WD_VIEW_MANAGER~BEFORE_WORKAREA_CONTENT_CHANGE, otherwise it won't work:
Step 2: redefine method DO_VIEW_INIT_ON_ACTIVATION
Again you will find coding example in main window of component BP_HIER. Here is the line we are interested in:
SET HANDLER on_before_wa_content_change ACTIVATION abap_true.
Step 3: implement method IF_BSP_WD_EVENT_HANDLER~HANDLE_EVENT
Last thing to do: add interface IF_BSP_WD_EVENT_HANDLER to your component main window, and implement method IF_BSP_WD_EVENT_HANDLER~HANDLE_EVENT. This method will be called when the user clicks on a button in the data loss popup, so you should implement your logic for each possible event:
You can very easily implement this data loss popup in your own components as soon as you implement those 3 methods above. I didn't mentioned a lot of coding on purpose because each implementation will be different. On the contrary: I recommend to have a look at standard components instead (like BP_HIER which is quite simple). I hope this wiki gave you enough hints to know where to look and find your way...
Remark: under some circumstances you may want to switch off the dataloss popup. For example when the main entity is deleted: most of the time you will navigate automatically outside of your component after the deletion, and you may not want to see any data loss popup... So create method BEFORE_CONTENT_CHANGE_OFF in your main window controller with the following code:
SET HANDLER on_before_wa_content_change ACTIVATION abap_false.
And call it after the successful deletion of your entity. Hence the method of your DELETE button in the relevant view may look like this:
IF lr_entity->lock( ) EQ 'X'.
" Switch off data loss popup...
lr_window_controller ?= me->view_manager->get_window_controller( ).