Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26172] Android: "focus" and "blur" events bubbles to parent views when it shouldn't

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2018-07-03T12:07:43.000+0000
Affected Version/sn/a
Fix Version/sRelease 7.3.0
ComponentsAndroid
Labelsandroid, blur, bubble, focus, parity
ReporterJoshua Quick
AssigneeJoshua Quick
Created2018-06-30T02:49:09.000+0000
Updated2018-07-03T21:57:06.000+0000

Description

*Summary:* A view's focus and blur events are not supposed to bubble up to the view's parents according to our documentation here... https://docs.appcelerator.com/platform/latest/#!/guide/Event_Handling-section-src-29004896_EventHandling-Bubblingandnon-bubblingevents On Android, they do bubble up. This can cause confusion since these events will bubble up to the window which use the focus and blur events to indicate if the window is active, not if it has gained/received input focus. This is not an issue on iOS. *Steps to reproduce:*

Build and run the below code on Android.

If the TextField has the focus, then tap on the "Remove Focus" button.

Clear the Android log.

Tap on the "Set Focus" button.

In the log, notice that the TextField focus event's "bubbles" property is set true. This is wrong.

In the log, notice the Window received a focus event after the TextField did. This is wrong.

Tap on the "Remove Focus" button.

In the log, notice that the TextField blur event's "bubbles" property is set true. This is wrong.

In the log, notice the Window received a blur event after the TextField did. This is wrong.

var window = Ti.UI.createWindow({
	layout: "vertical",
	fullscreen: true,
});
var textField = Ti.UI.createTextField({
	value: "Focus Test",
	width: Ti.UI.FILL,
	height: Ti.UI.SIZE,
});
textField.addEventListener("focus", function(e) {
	Ti.API.info("@@@ TextField 'focus' event received. bubbles: " + e.bubbles);
});
textField.addEventListener("blur", function(e) {
	Ti.API.info("@@@ TextField 'blur' event received. bubbles: " + e.bubbles);
});
window.add(textField);
var focusButton = Ti.UI.createButton({ title: "Set Focus" });
focusButton.addEventListener("click", function(e) {
	textField.focus();
});
window.add(focusButton);
var blurButton = Ti.UI.createButton({ title: "Remove Focus" });
blurButton.addEventListener("click", function(e) {
	textField.blur();
});
window.add(blurButton);
window.addEventListener("focus", function(e) {
	Ti.API.info("@@@ Window 'focus' event received.");
});
window.addEventListener("blur", function(e) {
	Ti.API.info("@@@ Window 'blur' event received.");
});
window.open();
*Expected Result:* The focus and blur events should not not bubble. Their bubbles property should be false and the parent window should not have received the TextField's events. *Work-Around:* Set the focus and blur event's cancelBubble property to true to prevent the event from propagating up the view hierarchy.
var textField = Ti.UI.createTextField();
textField.addEventListener("focus", function(e) {
	e.cancelBubble = true;
});
textField.addEventListener("blur", function(e) {
	e.cancelBubble = true;
});

Comments

  1. Joshua Quick 2018-06-30

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/10145
  2. Joshua Quick 2018-07-02

    PR (7.3.x): https://github.com/appcelerator/titanium_mobile/pull/10149
  3. Kota Iguchi 2018-07-02

    I'm suspecting this would happens on Windows too. I'll clone this ticket after checking on Windows.
  4. Lokesh Choudhary 2018-07-03

    Verified the fix in SDK 7.3.0.v20180703051211. Closing. Studio Ver: 5.1.0.201806291005 SDK Ver: 7.3.0.v20180703051211 OS Ver: 4.2.13 Xcode Ver: 7.0.4 Appc NPM: 1.1.3 Appc CLI: 10.13.5 Daemon Ver: Xcode 9.4.1 Ti CLI Ver: 5.1.1 Alloy Ver: 1.12.0 Node Ver: 8.9.1 NPM Ver: 5.5.1 Java Ver: 10.0.1 Devices: ⇨ google Nexus 5 --- Android 6.0.1 ⇨ google Nexus 6P --- Android 8.1.0 Emulator: ⇨ Android 4.1.2

JSON Source