Back button in toolbar not working in android fragment -


this question has answer here:

i enabled button in android fragment using below code.but when click on triggers side navigation menu.

@override public void oncreate(@nullable bundle savedinstancestate) {     super.oncreate(savedinstancestate);     sethasoptionsmenu(true); }  @override public void onresume() {     super.onresume();     ((appcompatactivity) getactivity()).getsupportactionbar().setdisplayhomeasupenabled(true);     ((appcompatactivity) getactivity()).getsupportactionbar().sethomebuttonenabled(true);  }  @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) {     super.oncreateoptionsmenu(menu, inflater); }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {         case android.r.id.home:             getactivity().onbackpressed();                 return true;             default:                 return super.onoptionsitemselected(item);         }     } 

try this:
use code inside class initialising toolbar , drawer layout.

getsupportfragmentmanager().addonbackstackchangedlistener(new fragmentmanager.onbackstackchangedlistener() {     @override     public void onbackstackchanged() {         if (getsupportfragmentmanager().getbackstackentrycount() > 0) {             drawerfragment.mdrawertoggle.setdrawerindicatorenabled(false);             getsupportactionbar().setdisplayhomeasupenabled(true);// show button             toolbar.setnavigationonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     onbackpressed();                 }             });         } else {             //show hamburger             drawerfragment.mdrawertoggle.setdrawerindicatorenabled(true);             getsupportactionbar().setdisplayhomeasupenabled(false);             drawerfragment.mdrawertoggle.syncstate();             toolbar.setnavigationonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     drawerfragment.mdrawerlayout.opendrawer(gravitycompat.start);                 }             });         }     } }); 

replace fragment below:

fragment fragment = gethomefragment(); fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.setcustomanimations(android.r.anim.fade_in,     android.r.anim.fade_out); fragmenttransaction.replace(r.id.frame, fragment, current_tag); if (fragment != new homefragment()){ fragmenttransaction.addtobackstack(null); //this line important } fragmenttransaction.commit(); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -