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}
[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.
PR ready here: https://github.com/appcelerator/titanium_mobile/pull/1104 Note: case 1 should not work
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.