android - adding back button support in a activity while using variables of a fragment -
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); fragment_view frag=new fragment_view(); fragmentmanager manager=getfragmentmanager(); fragmenttransaction transaction=manager.begintransaction(); transaction.add(r.id.fragment,frag,"hey"); transaction.commit(); } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { if (incustomview()) { hidecustomview(); return true; } if ((mcustomview == null) && webview.cangoback()) { webview.goback(); return true; } } return super.onkeydown(keycode, event); }
now here variables incustomview , hidecustomview defined in fragment. how add button support on activity while using variables fragment. want implement button support in android activity while using defined variables fragment.
public class fragment_view extends fragment{ private webview webview; private framelayout customviewcontainer; private webchromeclient.customviewcallback customviewcallback; private view mcustomview; private mywebchromeclient mwebchromeclient; private mywebviewclient mwebviewclient; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub view view= inflater.inflate(r.layout.fragment_view, container,false); customviewcontainer = (framelayout)view.findviewbyid(r.id.customviewcontainer); webview = (webview)view. findviewbyid(r.id.webview); mwebviewclient = new mywebviewclient(); webview.setwebviewclient(mwebviewclient); mwebchromeclient = new mywebchromeclient(); webview.setwebchromeclient(mwebchromeclient); webview.getsettings().setjavascriptenabled(true); webview.getsettings().setappcacheenabled(true); webview.getsettings().setbuiltinzoomcontrols(true); webview.getsettings().setsaveformdata(true); string htmlfile = "file:///" + environment.getexternalstoragedirectory() .getpath() + "/video_nsta/index.html"; webview.loadurl(htmlfile); return view; } public boolean incustomview() { return (mcustomview != null); } public void hidecustomview() { mwebchromeclient.onhidecustomview(); } @override public void onpause() { super.onpause(); webview.onpause(); } @override public void onresume() { super.onresume(); webview.onresume(); } @override public void onstop() { super.onstop(); if (incustomview()) { hidecustomview(); } } class mywebchromeclient extends webchromeclient { @override public void onshowcustomview(view view, int requestedorientation, customviewcallback callback) { onshowcustomview(view, callback); } @override public void onshowcustomview(view view,customviewcallback callback) { if (mcustomview != null) { callback.oncustomviewhidden(); return; } mcustomview = view; webview.setvisibility(view.gone); customviewcontainer.setvisibility(view.visible); customviewcontainer.addview(view); customviewcallback = callback; } @override public void onhidecustomview() { super.onhidecustomview(); if (mcustomview == null) return; webview.setvisibility(view.visible); customviewcontainer.setvisibility(view.gone); mcustomview.setvisibility(view.gone); customviewcontainer.removeview(mcustomview); customviewcallback.oncustomviewhidden(); mcustomview = null; } } class mywebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview view, string url) { return super.shouldoverrideurlloading(view, url); } } }
it's matter of distributing button.
first create fragment can handle it:
public abstract class backbuttonfragment extends fragment{ public boolean onbackpressed(){ return false; // defaults false } }
then change fragments extend backbuttonfragment
.
then on activity use appropriate callback backbutton (remove onkeydown
activity , use instead):
@override public void onbackpressed() { fragment f = getfragmentmanager().findfragmentbyid(r.id.fragment); if(f instanceof backbuttonfragment){ if(((backbuttonfragment)f).onbackpressed()) return; } // if fragment didn't use button normal stuff super.onbackpressed(); }
then inside each 1 of fragments override
method onbackpressed
, return correct value true
or false
depending on needs.
Comments
Post a Comment