androidurlwebviewtilt

android webview tilt screen page refreshing


I am using webview in my android app. It loads a webpage successfully.When I click on a link, it opens the new page in the webview and not in browser. Now my problem is that on tilting the screen the page gets refreshed and again the homepage is loaded instead of the page where we were earlier. How can I overcome this problem? Thanks in advance..my code is given below:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.xyz.com");
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.setWebViewClient(new MyWebViewClient());
}

my webviewclient class:

public class MyWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
}}

Solution

  • Well, that's because when the orientation changes, the activity is finished and restarted. So the onCreate() is called again and the first URL is loaded. You will have to save the state of the webview and restore it on orientation change.