Xamarin.Forms - Preventing double click
Hi, I'm back! Today I'm going to talk about a situation I faced this week and had to come up with a solution. I'm going to talk about double clicking on mobile navigation (if we can call it that).
When releasing an app for testing, some users said “look what I managed to do…”. So, I made that face of “what did you guys do this time…” and went to check. I identified that the user had clicked 2x on a certain button, causing the requested page to open 2x. In this reported case, the user was not even at fault or malicious. Unintentionally (I prefer to believe that!), he clicked quickly more than once on the button and the problem happened. It is worth mentioning that the app did not crash, it just opened the same page more than once, something similar to this:
With that, I remembered that when I started working with mobile development, I had already asked this to a developer who worked at Xamarin and he replied the following:
For the button issues, you may need to modify the events handling the button click event so that it disables the button after it has been clicked once. Although, in a real case your users should not be performing a "double-click" type of event.
And now, what to do? Trust the user?? Believe that he really won’t want to troll you and “go doing” what he shouldn’t in the app???
I decided not to trust, so I implemented a simple validation in navigation to solve this problem.
The solution
The solution I will mention takes into account that we have a centralized navigation system, so it was easy to implement this validation.
The scenario is as follows: we have 2 pages, Page1View and Page2View. On Page1View, we have a button. This button has a command, which when clicked, will call Page2View, as can be seen below in the view (xaml and code behind):
And below is the implementation of this page’s view model:
Notice that in the execution of the command, the algorithm uses DependencyService to load the implementation of INavigationHelper, and thus consume the GotoPage2 method to navigate to the next page. In this interface are all navigations that the app will have, in this case we have only 1 page:
In App.xaml.cs, I registered the dependency, informing DependencyService that to resolve INavigationHelper interface it should use NavigationHelper implementation.
And here is the implementation of NavigationHelper:
In the implementation of NavigationHelper, there is a private method called Navigate, and the goal here is that all page calls are executed by this method, because the “trick” is in the validation that exists in it, where it is checked if it is already being navigated. If so, the operation is aborted. Otherwise, the _isNavigating attribute is changed to true, the operation is executed and after 500 milliseconds the original state of the attribute is returned, _isNavigating = false.
Ready, problem solved. And then, what did you think? Ah, in this case, I was only able to simulate this problem on Android, ok? On iOS the problem did not happen with the Button component used in the project.
Maybe this solution is not so elegant, but it solved the problem and users will no longer be able to do that!
If you have a better idea or know a more refined implementation regarding this, please leave your comment.
The link to the example project I created can be found on Github.
Hug!