android - Fragment webview back button not working -


at navigationdrawer menu , clicking search opens specific webview in fragment . works fine, when press on webview - instead of getting last visited webscreen comes main fragment. how can solve this? i'm providing fragment java file here. please mention other codes need (if needed) solve problem


public class googlefragment extends fragment {  public webview mwebview;  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {      view v = inflater.inflate(fragment_google, container, false);     mwebview = (webview) v.findviewbyid(webview);     mwebview.loadurl("https://google.com");      // enable javascript     websettings websettings = mwebview.getsettings();     websettings.setjavascriptenabled(true);     //improve webview performance     mwebview.getsettings().setrenderpriority(websettings.renderpriority.high);     mwebview.getsettings().setcachemode(websettings.load_cache_else_network);     mwebview.getsettings().setappcacheenabled(true);     mwebview.setscrollbarstyle(view.scrollbars_inside_overlay);     websettings.setdomstorageenabled(true);     websettings.setlayoutalgorithm(websettings.layoutalgorithm.narrow_columns);     websettings.setusewideviewport(true);     websettings.setsavepassword(true);     websettings.setsaveformdata(true);     websettings.setenablesmoothtransition(true);      // force links , redirects open in webview instead of in browser     mwebview.setwebviewclient(new webviewclient());      return v; }  } 

i think when press button fragment need handle inside of activity,

you need first override onbackpressed() method in main activity having fragment , global assign webview public in google fragment

note : when adding or replacing fragment keep adding tag, can helpful getting instance of fragment.

@override     public void onbackpressed() {      fragment fragment =       getfragmentmanager().findfragmentbytag("google_fragment");      if (fragment instanceof googlefragment) {         if (webview.cangoback()) {             webview.goback();         } else {             super.onbackpressed();         }     }else{        super.onbackpressed();     } } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -