I want to go the previous MENU whenever I press the physical back button on my droid, could someone guide me through that ?
<p>This is my Function that dynamically create menu from a parsed XML, now I want that when I press the physical back button it should move to the previous menu.
I have read a lot of articles on this but still haven't find a single for going back in Menus.</p>

<pre><code>public int create_menu(int level_n){

    int lv=level_n;


    if(flag){
        base=0;
    }
    else{
        base=lv-1;
    }
    ctr=true;
    if(e[lv].getType().equals("menu")){
        flag=false;
        subm[lv]=subm[base].addSubMenu(0, lv, 0, e[lv].getName());
        lv++;




        for(int i=0;i&lt;noChild[lv-1];i++){
            if(e[lv+i].getType().equals("menu")){

                create_menu(lv+i);
                Log.i(TAG,"number=  "+i);
                flag=false;

            }
            else if (e[lv+i].getType().equals("action")){
                if(ctr){
                Log.i(TAG,"going to else"+e[lv].getName()+"   " +i);
                subm[lv]=subm[lv-1+i].addSubMenu(0, lv+i, 0, e[lv+i].getName());
                //Log.i(TAG,"element = "+e[lv+1].getType() );


                lv++;
                create_menu(lv);
                ctr=false;
                }

                flag=false;
            }
        }
        flag=true;

    }
    else if(e[lv].getType().equals("action")){
        if(flag==true &amp;&amp; ctr==true)
        {
            subm[lv]=subm[base].addSubMenu(0, lv,0, e[lv].getName());
            lv++;

        }
        else if(flag==false &amp;&amp; ctr==false){
            subm[lv]=subm[base-1].addSubMenu(0, lv, 0, e[lv].getName());
            lv++;

        }

    }

    return lv;


}
</code></pre>
