android - Freeze or make Stable Web-view When I Select Action Bar Back?Prevent Loading/reloading every time -
i want prevent page reload or freeze current web-view when select menu option menu
this webview
public class mywebv extends appcompatactivity { private webview webview; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mwview); toolbar toolbar = (toolbar) findviewbyid(r.id.tb1); setsupportactionbar(toolbar); webview = (webview) findviewbyid(r.id.web5); websettings set = webview.getsettings(); webview.setwebviewclient(new webviewclient()); webview.setwebchromeclient(new webchromeclient()); set.setjavascriptenabled(true); set.setjavascriptcanopenwindowsautomatically(true); set.setloadwithoverviewmode(true); set.setusewideviewport(true); set.setdomstorageenabled(true); set.setallowuniversalaccessfromfileurls(true); set.setjavascriptcanopenwindowsautomatically(true); webview.setscrollbarstyle(webview.scrollbars_outside_overlay); final alertdialog alertdialog = new alertdialog.builder(this).create(); final progressdialog progressbar = progressdialog.show(mywebv.this, "please wait", "loading..."); webview.requestfocusfromtouch(); webview.setwebviewclient(new webviewclient() { public void onloadresource (webview view, string url) { if (progressbar == null) { progressbar.settitle("please wait !"); progressbar.setmessage("loading..."); progressbar.show(); } } public void onpagefinished(webview view, string url) { try{ if (progressbar.isshowing()) { progressbar.dismiss(); } }catch(exception exception){ exception.printstacktrace(); } } }); webview.setwebchromeclient(new webchromeclient() { string url = "www.example.com/login.php"; webview.loadurl(url); } @override protected void onsaveinstancestate(bundle outstate){ super.onsaveinstancestate(outstate); webview.savestate(outstate); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); webview.restorestate(savedinstancestate); } @override public void onconfigurationchanged(configuration newconfig){ super.onconfigurationchanged(newconfig); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.m_mywebv, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.about1) { intent aboutintent = new intent(this, about.class); startactivity(aboutintent); return true; } else if (id == r.id.set1) { intent webintent = new intent(this, settings.class); startactivity(webintent); return true; } return super.onoptionsitemselected(item); } } }
i web-view have menus when click on menu setting or about... showing after when go home option or arrow action bar or option menu reloading
so need login , and check previous page difficult every time.. can 1 suggest me how freeze web-view or how prevent reload same app menus...
update
this class
public class extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.aboutus); toolbar toolbar = (toolbar) findviewbyid(r.id.tb3); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().setdisplayshowhomeenabled(true); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.m_about, menu); /*something*/ return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.home) { intent mypintent = new intent(this, mywebv.class); about.this.finish(); startactivity(mypintent); return true; } else if (id == r.id.about1) { intent aboutintent = new intent(this, about.class); about.this.finish(); startactivity(aboutintent); return true; } return super.onoptionsitemselected(item); }
}
so when click on arrow reloading webview.. on device hardware have disabled... on hardware fine but... on action bar reloading
i have performed test using 2 activities, mainactivity containing webview yours, , menu button opens second activity (about.java), using exact same intent used in onoptionsitemselected().
after opening activity menu, if press phone's hardware button, returns mainactivity without reloading contents of webview, it works expect.
i think the problem experiencing code use go main activity. may using intent open main activity again secondary activities, causing new instance of main activity created, triggering oncreate(), , re-creating webview , reloading page.
to test if that's case, can add debugging statements inside oncreate() using log.d() or system.out.println(), check if it's being called when try return main activity (mywebv). if see debug messages in logs, it's because you not returning main activity properly.
the way should close secondary activities return main activity calling:
finish()
that method close current activity , restore previous activity in stack (your mywebv activity in case), without re-creating it, calling onstart() , onresume(), wouldn't re-create webview or reload page.
Comments
Post a Comment