java - Android submenu: how to go back to previous submenu (or to main menu from submenu) -
beginner trying organize menu... have problem implement "back"-option in submenus. expect lead user automatically main menu. couldn't find solution. did has same challenge before?
i use xml context menu:
<item android:icon="@android:drawable/ic_menu_sort_by_size" android:title="menu" android:id="@+id/main_main_menu" app:showasaction="always"> <menu> <item android:id="@+id/goto_main_menu" android:orderincategory="1" android:title="@string/action_goto" android:icon="@android:drawable/ic_menu_directions"> <menu> <item android:orderincategory="1" android:title="new measurment" android:icon="@android:drawable/ic_menu_add"/> <item android:orderincategory="2" android:title="archive" android:icon="@android:drawable/ic_menu_agenda"/> <item android:id="@+id/back_main_menu" android:orderincategory="3" android:title="back" android:icon="@android:drawable/ic_menu_revert"/> </menu> </item> <item android:orderincategory="4" android:id="@+id/logout" android:title="@string/action_logout" android:icon="@android:drawable/ic_menu_set_as"/> </menu> </item>
i use following code display menu:
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; }
option click managed following code:
@override public boolean onoptionsitemselected(menuitem item) { if (id == r.id.back_main_menu) { toast.maketext(mainactivity.this, "let's go back...", toast.length_long).show(); return true; } if (id == r.id.logout) { intent intent = new intent(this, mainactivity.class); startactivity(intent); } return super.onoptionsitemselected(item); }
what should add code return main menu after clicking "back"??
try using onbackpressed() superclass of activity like
@override public boolean onoptionsitemselected(menuitem item) { if (id == r.id.back_main_menu) { super.onbackpressed(); return true; } // ... return super.onoptionsitemselected(item); }
if doesn't work, have give me more informations. if use several activities, call finish() method this:
@override public boolean onoptionsitemselected(menuitem item) { if (id == r.id.back_main_menu) { this.finish() return true; } // ... return super.onoptionsitemselected(item); }
finish() close actual viewed activity , navigate last known / viewed activity.
edit1 if have 1 activity, use fragments dynamically inflate views activity. @ example developer page: https://developer.android.com/training/basics/fragments/fragment-ui.html
edit2 if want different menu items appear, use onprepareoptionsmenu(). everytime want new menu, call invalidateoptionsmenu this:
@override public boolean onoptionsitemselected(menuitem item) { if (id == r.id.back_main_menu) { invalidateoptionsmenu(); return true; } // ... return super.onoptionsitemselected(item); }
furthermore have define/override onprepareoptionsmenu method in activity:
public void onprepareoptionsmenu(menu menu) { if(mainmenudisplay) // if main menu shall displayed { // use id's of menu entries enable or disable visibility menu.finditem(r.id.main_menu_entry1).setvisible(true); menu.finditem(r.id.other_menu_entry1).setvisible(false); } return true; }
Comments
Post a Comment