Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent Chrome from reloading page on re-orientation?
    primarykey
    data
    text
    <p>This is my first app, it is a web app, it simply loads a webView to a url where stuff happens.</p> <p>It works fine in android browser(gingerbread), but in Chrome(ICS, JB) it was going back to the initial webView url when the device is rotated. In gingerbread you account for this by setting android:configChanges="keyboard|keyboardHidden|orientation" and overriding onConfigurationChanged.</p> <p>After searching the web, I re-wrote my original activity as per the example here: <a href="http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/" rel="nofollow">http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/</a> , in which you essentially handle the re-orientation yourself.</p> <p><b>However now my app is crashing when I rotate the device.</b></p> <p>Testing with Android 4.1.2.</p> <p>Anyone see anything off with my code? Sorry if the answer is obvious, but I've been stuck on this bug for a couple of days now. Maybe I just need another set of eyes on it. Thanks in advance!</p> <p><i>Note: the line @SuppressWarnings("deprecation") is not in the example, it was suggested by the editor in order to compile.</i></p> <p>The Main Activity:</p> <pre><code>package com.example.xxx.xxx; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.content.res.Configuration; import android.view.Menu; import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; @SuppressLint("SetJavaScriptEnabled") public class MainActivity extends Activity { protected FrameLayout webViewPlaceholder; protected WebView myWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } @SuppressWarnings("deprecation") protected void initUI() { webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder)); if (myWebView == null){ //Create It myWebView = new WebView(this); myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(true); webSettings.setBuiltInZoomControls(true); webSettings.setLoadsImagesAutomatically(true); myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); myWebView.setScrollbarFadingEnabled(true); myWebView.setWebViewClient(new WebViewClient()); myWebView.setWebChromeClient(new WebChromeClient()); myWebView.setInitialScale(1); myWebView.loadUrl("http://www.theurl.com"); } webViewPlaceholder.addView(myWebView); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onConfigurationChanged(Configuration newConfig){ if (myWebView != null) { webViewPlaceholder.removeView(myWebView); } super.onConfigurationChanged(newConfig); setContentView(R.layout.activity_main); initUI(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the state of the WebView myWebView.saveState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore the state of the WebView myWebView.restoreState(savedInstanceState); } } </code></pre> <p>Main Activity XML:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;FrameLayout android:id="@+id/webViewPlaceholder" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload