android - toolbar actions triggering settings menu -
i have toolbar typical settings activity attached 3 dot menu.
in 1 of fragments change toolbar add couple of icons, when these icons pressed runs method , launches typical settings activity,
heres how call settings in main activity
@override public boolean oncreateoptionsmenu(menu menu) { if (!mnavigationdrawerfragment.isdraweropen()) { // show items in action bar relevant screen // if drawer not showing. otherwise, let drawer // decide show in action bar. getmenuinflater().inflate(r.menu.main, menu); return true; } return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem mitem) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = mitem.getitemid(); intent intent = new intent(mainactivity.this, settings.class); startactivity(intent); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(mitem); }
and here how add items , use them in fragment
@override public void oncreateoptionsmenu(menu menu,menuinflater inflater) { // inflate menu items use in action bar inflater.inflate(r.menu.set_menu, menu); mshare = menu.finditem(r.id.share); msave = menu.finditem(r.id.save); super.oncreateoptionsmenu(menu, inflater); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.share: share(); break; case r.id.save: savewallpaper(); return true; default: } return true; }
im still kinda new android , hoping rather trivial , help
your onoptionsitemselected()
unconditionally calls startactivity()
, rather calling when settings option selected. move lines within if statement:
@override public boolean onoptionsitemselected(menuitem mitem) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = mitem.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { intent intent = new intent(mainactivity.this, settings.class); startactivity(intent); return true; } return super.onoptionsitemselected(mitem); }
Comments
Post a Comment