Exiting an app is fine when data has been saved, but the inadvertant closing of an app can lead to lost work. Read on to find out how to use the ConfirmExit property to help prevent users losing unsaved changes
User Confirmation to Exit App
A canvas app can be closed by any of the following actions:
- Closing the browser tab in which the app is being run when using Windows
- Closing the whole browser in Windows
- Clicking an icon or other control that executes the Exit() function
- Swiping back to the list of apps when using a device running iOS and Android
The App.ConfirmExit property is a Boolean that can be set to ‘true’ or ‘false’. When set to ‘true’, a confirmation dialog pop-up is displayed on screen requesting confirmation to exit the app and discard unsaved changes, preventing the accidental loss of data
Confirmation pop-up message on Chrome
Confirmation pop-up message on Android
This is useful, but rather than simply setting App.ConfirmExit to ‘true’, a more subtle approach is to set the value depending on the status of a variable or control. For example, if the app has a form called Form1, then the App.ConfirmExit property can be set according to the saved status of the form as below
Form1.Unsaved
If Form1 is unsaved, this returns a value of true and if the user attempts to close the app then the confirmation dialogue is displayed. If the form has already been saved then the value returned is false and so the app just closes
If the app has 3 separate forms on 3 separate screens, and you want to make sure the form on the current screen has always been saved before exiting, this can be achieved using the Switch function
Assign the current screen name to a variable, in this case the variable varCurrentScreen, using each screen’s OnVisible property
Set(varCurrentScreen,App.ActiveScreen.Name)
Use the Switch function to check if the form on the current screen is unsaved by assigning the formula below to the App.ConfirmExit property
Switch(
varCurrentScreen,
"Screen1",
Form1.Unsaved,
"Screen2",
Form2.Unsaved,
"Screen3",
Form3.Unsaved
)
Now before exiting the app, if the form on the current screen is not saved, the dialog pop-up requesting confirmation to close the app is displayed
Some Microsoft documentation states that for App.ConfirmExit to reference any controls in your app other than those on the first screen, the ‘Delayed load’ preview feature must be disabled. I haven’t found that to be the case. The formula above references forms on 3 different screens and runs fine with ‘Delayed load’ either enabled or disabled
Custom Exit Message
When attempting to close the app when App.ConfirmExit is set to true, a generic message is provided by default
Another useful property of the App object is ConfirmExitMessage. This property can be used to define a custom text message to display in the confirmation pop-up
The message must be text contained within quotes. It’s not possible to make the exit message dynamic
Summary
Using these properties of the App object can remove the risk of users exiting an app without saving data
Bear in mind that the pop-ups are only displayed when the app is published and run on a device. Pop-ups aren’t displayed when the app is run in Test Studio
Displaying the pop-up dialogue box every time a user attempts to close the app is usually counter-productive. After a while, the dialogue box becomes an irritation and the message is unlikely to be read. For this reason, I recommend setting the App.ConfirmExit property dynamically so the dialogue box is only displayed when actually necessary