Therefore, I found a way to avoid nesting by calling Ui.popView() just before I would open a sub-menu. My strategy works, but actually I consider it inconsistent. Or maybe I just don't understand very well how Ui.popView(), Ui.Menu and Ui.MenuInputDelegate work.
Let me explain. Consider this code:
function myMenu1()
{
var menu1 = new Ui.Menu();
menu1.addItem("Open menu 2", :openMenu2);
menu1.addItem("Just something stupid", :justDoSomethingStupid); // 'somethingStupid' means not pushing another view
Ui.pushView(menu1, new menuDelegate(), Ui.SLIDE_IMMEDIATE);
}
function myMenu2()
{
Ui.popView(Ui.SLIDE_IMMEDIATE); // This pops "menu1" (as I wanted)
var menu2 = new Ui.Menu();
menu2.addItem("Dummy", :dummy);
Ui.pushView(menu2, new menuDelegate(), Ui.SLIDE_IMMEDIATE);
}
function justDoSomethingStupid()
{
Ui.popView(Ui.SLIDE_IMMEDIATE); // This pops the main view and closes the program
var aMenu = new Ui.Menu();
aMenu.addItem("Dummy", :dummy);
// Ui.pushView(aMenu, new menuDelegate(), Ui.SLIDE_IMMEDIATE); // (no view is pushed)
}
class menuDelegate extends Ui.MenuInputDelegate
{
function onMenuItem(item)
{
if
(item == :openMenu2)
{
myMenu2();
}
else if
(item == :justDoSomethingStupid)
{
justDoSomethingStupid();
}
}
}
In myMenu2(), the call to Ui.popView() closes the previous menu. It only works because there's a Ui.pushView() in the sequence.
In justDoSomethingStupid(), which is the same as myMenu2() but without the Ui.pushView(), the call to Ui.popView() closes the main view and finishes the program (assuming there is only one view).
It seems that there is an implicit call to Ui.popView() at the end of onMenuItem(), which is only made when no other menu is pushed. This pops the menu after some 'normal' action. If another call to Ui.popView() is made (such as in justDoSomethingStupid()), there'll be two Ui.popView()'s in the queue, and the second one would pop the main view. (Maybe there's another explanation? I'd be glad to hear...)
I'm happy that the strategy in myMenu2() works, but I consider it inconsistent. If the commands are executed sequentially, why should Ui.popView() affect different views when a Ui.pushView() appears later in the code?
My questions:
- Is this behavior planned? May I keep using my strategy?
- Are there other ways of avoiding nesting which are better or equivalent in terms of memory efficiency? I have a second strategy that uses a timer, but of course this is less efficient.
???????Cheers