android DrawerLayout navigation Fragment reload without click on DrawerLayout item -
i creating simple app having drawerlayout navigation. in 1 menu item my profile. in my profile screen there button open change password screen in same fragment. if open drawerlayout 's menu , close without clicking, my profile screen loads again.
following code
public class homeactivity extends actionbaractivity { private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private arraylist<sidemenuentity> listsidemenuitems; private sidemenuadapter adaptersidemenu; menu menu; fragment fragment; asynctaskhelper loadfragmenttask; int position, old_position; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); mtitle = mdrawertitle = gettitle(); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.list_slidermenu); listsidemenuitems = new arraylist<>(); for(string str : constant.side_menu_items) { listsidemenuitems.add(new sidemenuentity(str)); } // setting nav drawer list adapter adaptersidemenu = new sidemenuadapter(getapplicationcontext(),listsidemenuitems); mdrawerlist.setadapter(adaptersidemenu); mdrawerlist.setonitemclicklistener(new listview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { displayview(position); } }); actionbar actionbar = getsupportactionbar(); actionbar.setdisplayhomeasupenabled(true); actionbar.sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.mipmap.ic_launcher,r.string.app_name, r.string.app_name) { public void ondrawerclosed(view view) { getsupportactionbar().settitle(mtitle); invalidateoptionsmenu(); if (fragment != null) { _setfragmentcontainer(fragment, listsidemenuitems.get(position).title); } } public void ondraweropened(view drawerview) { getsupportactionbar().settitle(mdrawertitle); // calling onprepareoptionsmenu() hide action bar icons invalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { old_position = -1; displayview(-1); _setfragmentcontainer(fragment, listsidemenuitems.get(position).title); } } @override public boolean oncreateoptionsmenu(menu menu) { this.menu = menu; getmenuinflater().inflate(r.menu.menu_home_setting, menu); _setactionbarhomevisible(false); return true; } @override public boolean onoptionsitemselected(menuitem item) { // toggle nav drawer on selecting action bar app icon/title if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action bar actions click switch (item.getitemid()) { case r.id.title_bar_home: displayview(0); return true; case r.id.title_bar_setting: _setfragmentcontainer(new settingfragment(), "setting"); return true; default: return super.onoptionsitemselected(item); } } /*** * called when invalidateoptionsmenu() triggered */ @override public boolean onprepareoptionsmenu(menu menu) { return super.onprepareoptionsmenu(menu); } /** * diplaying fragment view selected nav drawer list item * */ protected void displayview(int p) { position = p >= 0 ? p : 0; fragment = null; switch (position) { case constant.side_menu_item_dahshboard: fragment = new dashboardfragment(); break; case constant.side_menu_item_my_profile: fragment = new myprofilefragment(); break; } mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); mdrawerlayout.closedrawer(mdrawerlist); } @override public void settitle(charsequence title) { mtitle = title; getsupportactionbar().settitle(mtitle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); mdrawertoggle.onconfigurationchanged(newconfig); } /**-------------- private functions ---------------------*/ public void _setfragmentcontainer(fragment fragment, string title) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.frame_container, fragment).commit(); settitle(title); }
}
try adding super constructor:
public void ondrawerclosed(view view) { super.ondrawerclosed(view); getsupportactionbar().settitle(mtitle); invalidateoptionsmenu(); if (fragment != null) { _setfragmentcontainer(fragment, listsidemenuitems.get(position).title); } } public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); getsupportactionbar().settitle(mdrawertitle); // calling onprepareoptionsmenu() hide action bar icons invalidateoptionsmenu(); }
Comments
Post a Comment