Android WebView white page after restart activity - android

I am using WebView to load a webpage in Android activity.
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.addJavascriptInterface(new Unlock(), "AndroidUnlock");
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/demo.html?ver=" + System.currentTimeMillis());
When the webview loaded, I pressed the back button to finish the activity.
And I launch the activity again, the webview load the webpage incorrectly. It shows a blank page to me.
How can I to fix the issue please?
If I use System.exit(0) to exit the activity, it works very well, the webview load the webpage correct again.
Regards

You are loading the URL in onCreate(). Put the code (except webView = (WebView) findViewById(R.id.webview);) in onResume() :
#Overrride
protected void onResume(){
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.addJavascriptInterface(new Unlock(), "AndroidUnlock");
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/demo.html?ver=" + System.currentTimeMillis());
}
Hope this helps.

Related

AdvancedWebView loading html from phone storage issue

Hi i am using compile 'com.github.delight-im:Android-AdvancedWebView:v3.0.0' for display html5, my target file is stored in my phone storage path is "file:///storage/sdcard0/DcLms/Conjunction/index.html". when I am working with normal android WebView no issues. But trying with above mentioned library i got Webpage not available issue.
String targetPath = "file:///storage/sdcard0/DcLms/Conjunction/index.html";
AdvancedWebView webView = (AdvancedWebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new HelloWebViewClient());
webView.loadUrl(targetPath);
WebView basicView = (WebView) findViewById(R.id.basicView);
webView.setWebViewClient(new HelloWebViewClient());
basicView.getSettings().setJavaScriptEnabled(true);
basicView.setWebChromeClient(new WebChromeClient());
basicView.getSettings().setDomStorageEnabled(true);
basicView.loadUrl(targetPath);
basicView.setWebViewClient(new HelloWebViewClient());
First of all AdvancedWebView configures all settings automatically on it's instantiating. Take a look on the sources here.
And the problem is that by default AdvancedWebView dissallow access using file:// scheme:
...
final WebSettings webSettings = getSettings();
webSettings.setAllowFileAccess(false);
setAllowAccessFromFileUrls(webSettings, false);
webSettings.setBuiltInZoomControls(false);
...
So after creation of AdvancedWebView you need to add following lines:
AdvancedWebView webView = (AdvancedWebView) findViewById(R.id.webView);
webView.getSettings().setAllowFileAccess(true);
if (Build.VERSION.SDK_INT >= 16) {
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
NOTE: AdvancedWebView just subclassing WebView so it will not make any difference - the only difference that it configurating settings for you and handle some of the events.

html5 scorm audio is not playing in android webview but it is playing in ios

#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.WebView1);
WebViewClient webViewClient = new WebViewClient();
webView.setWebViewClient(webViewClient);
webView.clearCache(true);
webView.getSettings().setUseWideViewPort(true);
webView.setInitialScale(1);
webView.getSettings().setBuiltInZoomControls(true);
webView.clearHistory();
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.setWebChromeClient(new WebChromeClient());
webView.addJavascriptInterface(new AudioInterface(this), "AndAud");
webView.loadUrl("file:///android_asset/web/index_SCORM.html");
}
}
I am trying to load the html5 scorm from the asset folder but the sound is not getting played on android. But in ios if I do the same the audio is getting played.
May I know the reason why this is happening?

How to remove or disable WebView header?

I'm trying to remove the webview header, I've tried a number of possible solutions but to no avail.
One of my attempts:
android:theme="#android:style/Theme.NoTitleBar"
I'm loading the webview using this code:
setContentView(R.layout.activity_websitenew1);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://www.domain.com");
myWebView .setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
What is happening in your case is that the URL is being handled by Android which is choosing the handler for you. This handler is the system's browser application. What you want is for control to stay within your app.
You can do this in code as follows:
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.domain.com");
See http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String) for details.

android webview unset default browser

I use the following to show a webpage in a webview
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView)findViewById(R.id.webView);
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webView.loadUrl("http://www.bbc.co.uk");
}
It works perfect and even has pinch and zoom however When i click to go to another webpage via a link it opens it in the default browser instead of the webview
How to i achieve this. I have read many articles on this but cant understand where i need to insert the commands
Any help appreciated
Mark
thanks very much for the link above the following works perfect
myWebView.loadUrl("http://someurl.com");
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
viewx.loadUrl(urlx);
return false;
}
});

How to enable Addressbar in Android WebView?

I´m actually using a WebView in my Application and it works pretty good.
Now I´d like to be able to change the actual URL, just like that Addressbar in the Android Stock browser, where you can see the URL and where you simply can change it.
How can I enable this bar? Or do I have to implement it myself?!
Thanks!
My Code looks like:
private void setupWebView() {
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webview.getSettings();
webSettings.setUserAgentString("foo");
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.this.setProgress(progress * 100);
}
});
}
Impossible:
The WebView class is an extension of Android's View class that allows
you to display web pages as a part of your activity layout. It does
not include any features of a fully developed web browser, such as
navigation controls or an address bar. All that WebView does, by
default, is show a web page.
http://developer.android.com/guide/webapps/webview.html
If you want to change URL by code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

Categories

Resources