Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-2970] Android: event listeners on modules and from heavyweight windows cannot show alerts.

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionWon't Fix
Resolution Date2017-07-28T19:30:21.000+0000
Affected Version/sRelease 3.0.0
Fix Version/sn/a
ComponentsAndroid
Labelsapi
ReporterBill Dawson
AssigneeIngo Muschenetz
Created2011-04-15T03:33:56.000+0000
Updated2017-07-28T19:30:21.000+0000

Description

If an event listener is put on to a module and the event listener itself is created in a heavyweight context -- or, more specifically, is created outside the root context -- that event listener, when invoked, will be unable to successfully display alert dialogs.

The problem here -- I believe -- is that the module, which is responsible for invoking the event, belongs to the root context (all modules are now "booted" in the root context.) So the root activity context is invoking the event listener, but the listener's android context is not the root activity.

To see a failcase, create a new Titanium project and make this its app.js:

Titanium.UI.setBackgroundColor('#000');
var win = Titanium.UI.createWindow({  
    title:'Test',
    backgroundColor:'#fff',
    exitOnClose: true
});
win.add( Ti.UI.createLabel({
    text: 'This is a lightweight window',
    left: 5, right: 5, top: 5, height: 40,
    color: 'black'
}) );

Ti.App.addEventListener('test1', function(){
    alert('This is an alert');
});

var btnTest = Ti.UI.createButton({
    title: 'Test alert() from event listener',
    top: 50, left: 5, right: 5, height: 40
});
btnTest.addEventListener('click', function() {
    Ti.App.fireEvent('test1', {});
});
win.add(btnTest);

var btn = Ti.UI.createButton({
    title: 'Open heavy window',
    top: 95, left: 5, right: 5, height: 40
});
btn.addEventListener('click', function() {
    Ti.UI.createWindow({
        url: 'win.js',
        backgroundColor: '#fff',
        fullscreen: false
    }).open();
});
win.add(btn);
win.open();

Then add a new file, win.js, and make this its contents:

var win = Ti.UI.currentWindow;
win.add( Ti.UI.createLabel({
    text: 'This is a heavyweight window',
    left: 5, right: 5, top: 5, height: 40,
    color: 'black'
}) );
Ti.App.addEventListener('test2', function(){
    alert('This is an alert');
});
var btnTest = Ti.UI.createButton({
    title: 'Test alert() from event listener',
    top: 50, left: 5, right: 5, height: 40
});
btnTest.addEventListener('click', function() {
    Ti.App.fireEvent('test2', {});
});
win.add(btnTest);

var btn = Ti.UI.createButton({
    title: 'Close',
    top: 95, left: 5, right: 5, height: 40
});
btn.addEventListener('click', function() {
    win.close();
});
win.add(btn);

Run the project and follow these steps:

  • At the first (root, lightweight) window, if you click the "Test alert() from event listener" button, you will successfully see the alert.
  • Then click "Open heavy window" to open a heavyweight window to run the same kind of test.
  • At the heavyweight window, if you click the "Test alert() from event listener" button, you won't see the alert. (If you then click the Close button, you might actually see the alert, as it has popped up over the root window.)

Comments

  1. Bill Dawson 2011-04-15

    Assigned to Don for milestoning.

  2. Junaid Younus 2012-08-29

    Tested on a Samsung Galaxy S2 using TiSDK 2.2.0v20120828153312, issue unable to reproduce. However, you get multiple alerts once you open/exit the heavyweight window. Related to TIMOB-2969.
  3. Lee Morris 2017-07-28

    Closing due to inactivity. If this issue still exists, please raise a new ticket.

JSON Source