Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-6648] Android: onCreateOptionsMenu Regressions in 1.9.0 from 1.7.5

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-03-11T22:27:16.000+0000
Affected Version/sRelease 1.8.0.1
Fix Version/sSprint 2011-52, Release 2.0.0, Release 1.8.1
ComponentsAndroid
Labelsmodule_android, qe-testadded, regression
ReporterDawson Toth
AssigneeAllen Yeung
Created2011-12-13T15:10:46.000+0000
Updated2012-03-11T22:27:16.000+0000

Description

Problem

The onCreateOptionsMenu is very brittle with heavyweight windows in master / 1.9.0 compared to 1.7.5. These two use cases no longer work: * Ti.Android.currentActivity.onCreateOptionsMenu does not work. * win.activity.onCreateOptionsMenu after "open" is called but before the open event fires does not work. These two use cases do work: * win.activity.onCreateOptionsMenu after the open event fires works. * win.activity.onCreateOptionsMenu before "open" is called.

Expected Behavior

Use cases that worked in 1.7.5 should work here as well.

Example Code

Drop the following in an app.js, and change the "useApproach" constant to the approach you want to see in action.
var win = Ti.UI.createWindow({
    backgroundColor: 'white',
    exitOnClose: true,
    navBarHidden: true
});

win.add(Ti.UI.createLabel({
    text: 'Press your Android device\'s menu button!',
    color: '#000'
}));

var useApproach = 1;
switch (useApproach) {
    case 1:
        /*
         WORKS in 1.7.5. BROKEN in 1.8.0.1.
         Does not work. Ti.Android.currentActivity won't be accurate because we created a heavyweight window above.
         */
        win.open();
        Ti.Android.currentActivity.onCreateOptionsMenu = function (e) {
            e.menu.add({ title: 'Approach 1', itemId: 1 });
        };
        break;
    case 2:
        /*
         WORKS in 1.7.5. BROKEN in 1.8.0.1.
         Does not work. It looks like "activity" is not accurate yet.
         */
        win.open();
        win.activity.onCreateOptionsMenu = function (e) {
            e.menu.add({ title: 'Approach 2', itemId: 2 });
        };
        break;
    case 3:
        /*
         WORKS in both 1.7.5 and 1.8.0.1.
         It looks like "activity" is accurate before we call "open".
         */
        win.activity.onCreateOptionsMenu = function (e) {
            e.menu.add({ title: 'Approach 3', itemId: 2 });
        };
        win.open();
        break;
    case 4:
        /*
         WORKS in both 1.7.5 and 1.8.0.1.
         If we wait until the open event fires, we can then define the onCreateOptionsMenu properly.
         */
        win.addEventListener('open', function (evt) {
            win.activity.onCreateOptionsMenu = function (e) {
                e.menu.add({ title: 'Approach 4', itemId: 3 });
            };
        });
        win.open();
        break;
}

Console

The following shows up in the console regardless of if the menu shows up or not: {quote} 12-13 15:09:27.082: E/imdg81(2450): IsShutDownStarted() 12-13 15:09:27.086: I/KeyInputQueue(2450): Input event 12-13 15:09:27.207: E/imdg81(2450): IsShutDownStarted() 12-13 15:09:27.207: I/KeyInputQueue(2450): Input event {quote}

Comments

  1. Dawson Toth 2011-12-13

    [TIMOB-6439] took care of not being able to bind the options menu after the window's "open" event fires. But there are two other use cases identified here that do not work.
  2. Allen Yeung 2011-12-27

    PR ready here: https://github.com/appcelerator/titanium_mobile/pull/1104 Note: case 1 should not work
  3. Wilson Luu 2012-01-12

    Closing bug. Verified fix on: SDK build: 1.9.0.v20120112104633 Runtime: V8, Rhino Titanium Studio, build: 1.0.8.201201111843 Device: Droid 3 (2.3.4) Note: Verified with Allen that case 1 in the attached code will not behave the same as in 1.7.5; i.e. case 1 will not work for 1.8.X, 1.9.X and up because underlying changes have been made.

JSON Source