android - NavigationView actionaBar onOptionsItemSelected R.id.home not triggered -


i want implement button in android application 1 activity changes content different fragment. used default navigation drawer activity provided android studio added framelayout content_main. in fragment want botton shown added line of code in oncreateview method:

actionbar actionbar = ((appcompatactivity)getactivity()).getsupportactionbar(); actionbar.setdisplayhomeasupenabled(true); 

ad line in oncreate method:

sethasoptionsmenu(true); 

and added method catch click of it:

 @override public boolean onoptionsitemselected(menuitem item) {     if (item.getitemid() == android.r.id.home) {         log.w("second fragment","clicked back");         return true;     }     return super.onoptionsitemselected(item); } 

in activiy set oncreateoptionsmenu this:

@override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; } 

but click of isn't triggered. tried add setting button , triggered. read lot of question can't figure out how resolve it

try using switch(...) case statement.

 @override public boolean onoptionsitemselected(menuitem item) {      //this make hamburger button clickable.     if (mdrawertoggle.onoptionsitemselected(item)) {         return true;     }      //this make homeasupindicator button clickable.     switch (item.getitemid()) {         case android.r.id.home:            log.w("second fragment","clicked back");            break;     }     return super.onoptionsitemselected(item); } 

hope you.


Comments