java - Jump to another activity when clicking a item in menu of actionbar in android studio -
this code select items in menu, when user click logout, activity jumps login_activity, when user clicks account, activity jumps account_activity.
@override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()){ case r.id.logout_id: intent login = new intent(mainpanelactivity.this, loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); case r.id.account_id: intent account =new intent(mainpanelactivity.this,accountactivity.class); startactivity(account); default: return super.onoptionsitemselected(item); } }
however, during testing, no matter click, activity jumps account activity.
if switch order of switch cases
@override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()){ case r.id.account_id: intent account =new intent(mainpanelactivity.this,accountactivity.class); startactivity(account); case r.id.logout_id: intent login = new intent(mainpanelactivity.this, loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); default: return super.onoptionsitemselected(item); } }
no matter click, activity jumps login_activity. knows why?
you need add break;
in each case below. otherwise code execute case satisfies switch condition , each case after until either break;
or end of switch
reached.
@override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()){ case r.id.account_id: intent account =new intent(mainpanelactivity.this,accountactivity.class); startactivity(account); break; case r.id.logout_id: intent login = new intent(mainpanelactivity.this, loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); break; default: return super.onoptionsitemselected(item); } }
Comments
Post a Comment