Different behavior with focus() between Android and iOS
Hello together, I have a page with a sap.m.Input field. For this field I want to set the focus out of the controller. I have tried several approaches. But it works only with the help of setTimeout I gues. Getting the control via this.getView().byId('control') to set the focus does not work reliably. On a desktop PC and on Android mobile devices this setTimeout approach works. On an iOS device unfortunately not. Sometimes only a small highlighted frame is visible, but not every time on iOS. Background would be a barcode scanning app.
Here is the code that currently works.
onAfterRendering: function () { const oScanField = this.byId("scanField"); setTimeout(() => { oScanField.focus(); }, 250); },
Are there certain requirements for the focus or does a certain sequence have to be followed for a simple focus() to work out of the controller?
Would it also be possible to set the focus() and hide the soft keyboard initially? Only when the user actively clicks into the input field the keyboard should be shown. So when the focus is set from the controller, then the softkeyboard should be hidden and when the user manually clicks into the field, then the keyboard should be displayed.
Here I have found a work-a-round. Is there a better approach for this?
onTestPress: function () { const oScanField = this.byId("scanField"); // todo https://answers.sap.com/questions/12963714/sapui5-how-to-disable-soft-keyboard-on-android-whe.html // currently just a workaround for hiding the soft - keyboard on the scanner oScanField.setEditable(false); setTimeout(() => { oScanField.setEditable(true); oScanField.focus(); }, 200); },
Current UI5 framework are: 1.108.16
Test on Desktop with Browser Edge and Chrome (latest version) • focus() is working. • No Keyboard test
Test on Android with Edge and Chrome (latest version) • focus() is working • Soft keyboard is hidden for the focus() out of the controller
Test on iOS with Safari and Chrome (also latest version) • focus() not working. Sometimes a dotted frame is visible, but not very often. • Soft Keyboard is not hidden.
Here the result on the desktop PC as picture:
Here the result on Android as picture:
Here the result on iOS as picture:
Maybe I did something wrong or the implementation needs to be changed from my side. Maybe there is another and more stable approach for my implementation.
Thanks for support
Hello @pa775,
Thank you for reaching out!
Unfortunately I was not able to reproduce the issue following your description. Could you please provide an isolated example where we can reproduce and examine it?
As for the soft keyboard, I believe we can not do anything on our side to prevent this as this is specifically OS logic and we have no influence on it. I believe the OS providers have exported an API to cover this case, but I'm not sure.
Thank you in advance! Regards, Ivaylo
I don't know if I'm wrong, but my current app is a complex one with al lot of company data inside. But I my mind it is easy to follow. If I define the function onAfterRendering I just get the INPUT by this.getView().byId('inputField'). After that I call inputField.focus(). The cursor is not "blinking" in the input field. This appears on every device (PC, Android and iOS).
After modify the onAfterRendering function to:
onAfterRendering: function () { const oScanField = this.byId("scanField"); setTimeout(() => { oScanField.focus(); }, 250); }
the cursor is blincing inside the input field. See screenshots above. This will work on PC and Android but not on iOS.
Therefor I think this is a very small function to implement and test.
Hope my description will help.
BG
Hello @pa775 ,
I have created an isolated example as you have described. I have simply added a Button and an Input in an XML view and onAfterRendering I am setting the focus to the Input field and it works as expected in all browsers, without the need to wrap in into a timeout. This makes me think that you probably have some kind of a race condition in the app which you try to overcome with the timeout, however this won't work everytime.
In order for me to further support you, I would need a steady reproducible example.
Regards, Ivaylo
Hello,
I will strip out the company parts form my app and try to upload my example. But I guess it will be tomorrow.
BG Sascha
This is the test webapp without setTimeout. Hope this will help.
Not everything was cleared, but this should be starting point. I have tested it again, and after refresh or loading the page the focus is not set. If I make use of setTimeout it will work on loading or refresh the page.
Hope you can see the same effect.
Some I have forgotten. I also tested this small demo app on iOS. Also here no focus is set, independent if the focus was directly set or with a setTimeout. Then I have create a small function for a button press. In this function I will set the focus to the field. This will work on PC but not on my Android scanner or on iOS. To keep it short, only when I make use of setTimeout, also the focus can be set on native Android devices, but this also do no work in iOS. On Android. When the focus was set, sometimes the soft keyboard comes up sometimes the keyboard doesn't come up. Also very strange to me. Maybe a different lifecycle function must be used, something after onAfterRendering. But I don't know.
I hope my description will help to get a clue on this.
Hello @pa775,
Actually the focus is being set as expected, but it's then stolen by the button in the toolbar. Please revise your application logic and see why this is happening.
Since the issue is not on our side, I am closing the issue.
Best regards, Ivaylo
What du you mean when you said "stolen by the button in the toolbar"? Because the buttons are only for manually user interactions and not triggered automatically. If I'm wrong for sure you can close the ticket, but please let me know where you find the automatic toolbar action.
Thx Sascha
Hi again,
The issue you have reported initially is that .focus() is not working. If you put a debugger in the "onAfterRendering" handler, you can see that the focus is set correctly, however for some reason the focus moves to the button. You need see why this is happening by yourself and if you consider this to be another issue, please raise another incident with a proof that this is a framework issue and not an application one.
Regards, Ivaylo