java - How to enable back press to close nav drawer on fragment only -
i have implemented navigation drawer fragment hence know what's best way implement pressed feature fragments code needs go? i've used pressed code below doesn't work.
activity class
public class bakerloohdnactivity extends appcompatactivity { //save our header or result private drawer result = null; private int getfactorcolor(int color, float factor) { float[] hsv = new float[3]; color.colortohsv(color, hsv); hsv[2] *= factor; color = color.hsvtocolor(hsv); return color; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_bakerloo_hdn); localbroadcastmanager.getinstance(this).registerreceiver(onnotice, new intentfilter("backpressed_tag")); final string actionbarcolor = "#b36305"; toolbar mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); if(getsupportactionbar()!=null) { getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().setdisplayshowhomeenabled(false); getsupportactionbar().settitle(html.fromhtml("<font color='#ffffff'>" + getresources().getstring(r.string.hdn) + "</font>")); final drawable uparrow = contextcompat.getdrawable(this, r.drawable.abc_ic_ab_back_mtrl_am_alpha); uparrow.setcolorfilter(getresources().getcolor(r.color.white), porterduff.mode.src_atop); getsupportactionbar().sethomeasupindicator(uparrow); } if (build.version.sdk_int >= build.version_codes.lollipop) { window window = getwindow(); window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds); getwindow().setstatusbarcolor(getfactorcolor(color.parsecolor(actionbarcolor), 0.8f)); } // start of navigation drawer accountheader headerresult = new accountheaderbuilder() .withactivity(this) .withcompactstyle(true) .withheaderbackground(r.color.bakerloo) .withprofileimagesvisible(false) .withtextcolor(color.parsecolor("#ffffff")) .withselectionlistenabled(false) .addprofiles( new profiledraweritem().withname(getstring(r.string.hello)).withemail(getstring(r.string.world)) ) .build(); result = new drawerbuilder() .withactivity(this) .withaccountheader(headerresult) .withtranslucentstatusbar(false) .withactionbardrawertoggle(false) .withselecteditem(-1) .adddraweritems( new sectiondraweritem().withname(r.string.other_lines_nocaps).setdivider(false), new primarydraweritem().withname(r.string.hello_world).withidentifier(1).withcheckable(false) ) .build(); // end of navigation drawer } @override public void onbackpressed() { if (result.isdraweropen()) { result.closedrawer(); } else { super.onbackpressed(); } localbroadcastmanager.getinstance(context).sendbroadcast(new intent("backpressed_tag")); } }
fragment class
public class fragmentbakerloohdn extends android.support.v4.app.fragment { public fragmentbakerloohdn() { // required empty constructor } broadcastreceiver onnotice = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { // stuff when in activity pressed headerresult.closedrawer(); } }; // declaring navigation drawer private accountheader headerresult = null; private drawer result = null; /** * whether or not activity in two-pane mode, i.e. running on tablet * device. */ private boolean mtwopane; @override public void oncreate(bundle savedinstancestate) { localbroadcastmanager.getinstance(getactivity()).registerreceiver(onnotice, new intentfilter("backpressed_tag")); super.oncreate(savedinstancestate); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_bakerloo_hdn, container, false); headerresult = new accountheaderbuilder() .withactivity(getactivity()) .withcompactstyle(true) .withheaderbackground(r.color.bakerloo) .withprofileimagesvisible(false) .withtextcolor(color.parsecolor("#ffffff")) .withselectionlistenabled(false) .addprofiles( new profiledraweritem().withname(getstring(r.string.hdn)).withemail(getstring(r.string.hello_world)) ) .build(); result = new drawerbuilder() .withactivity(getactivity()) .withaccountheader(headerresult) .withtranslucentstatusbar(false) .withactionbardrawertoggle(false) .withselecteditem(-1) .adddraweritems( new primarydraweritem().withname(r.string.hello_world).withidentifier(1).withcheckable(false) ) .build(); super.oncreate(savedinstancestate); return v; } @override public void onactivitycreated(bundle savedinstancestate) { view v = getview(); super.onactivitycreated(savedinstancestate); } }
there's no onbackpressed()
in fragment
. it's activity's method. can do, can make activity call fragment method when onbackpressed()
called:
@override public void onbackpressed() { if (!frag.onbackpressed() ) { super.onbackpressed(); } }
and fragment's onbackpressed() want , return
truewhen done anything, or
false` otherwise, can call super's 1 in such case.
Comments
Post a Comment