OnVisible of a screen is a much used location for your logic (code). After the code is run, that screen is in a “start” mode from where people can take further actions.
Having the same logic (code) on multiple places is from a maintainability perspective not desired. So how can you run the code in the property
OnVisible of a screen again to get the screen in its “start” mode?
A redirect screen to the rescue!
Code in the property
OnVisible is only run when you navigate to that screen. There is no way to run it while you are on the screen. Using a redirect screen, you temporary navigate to another screen (the redirect screen which is called scr_Redirect in this blog post) to immediately switch to the screen you want to go to, which could be the screen you originated from and thus running the “OnVisible” code again.
There must be a place for a user to trigger this redirect action from. A menu where each menu option navigates to a specific screen is a common way.
The steps below explain how to create a redirect screen.
1) When clicking on a menu option, set a global variable (glb_RedirectScreen for this blog post) so it contains the screen object of the screen you want to navigate to. Code from example canvas app:
Set(glb_RedirectScreen, ThisItem.Screen); Navigate(scr_Redirect);
2) On the app control, in the property
OnStart, set a global variable (glb_Redirect for this blog post) to true. When developing the canvas, you can easily update the value of this variable to false and run the
OnStart of the app control again if you want to make changes to the redirect screen. Code from example canvas app:
Set( glb_Redirect, true );
3) Create a new screen and give it a proper name.
4) Add a button on that screen, give it a proper name (btn_Redirect for this blog post) and set the property
I normally place such button in the top left.
5) Add the following code to the property
OnVisible of the redirect screen:
6) Put the following code in the property
OnSelect of the button:
If( glb_Redirect, Navigate(glb_RedirectScreen) )
And that is all there is to it.
The variable glb_Redirect is important for a redirect screen. When such a construction is not used, the redirect screen could never be changed again because you will always be redirected. In case you forgot this, a fix would be the creation of a new screen with a button which sets the global variable glb_RedirectScreen to the redirect screen. But I prefer the way described above.
An example canvas app has been created (export) in support of this blog post and is downloadable from my GitHub repository here.
A video in support of this blog post can be viewed on my YouTube channel here.
As an alternative, one can also work with only a reset button per screen. A reset button is a button which:
- is not visible.
- contains the logic (code) you would normally put in the screen property
- Is called from the screen property
OnVisiblewith the function Select. Example:
This alternative does not work well with a menu imo and that’s why I prefer the redirect screen setup.