Search code examples
androidwebviewscreen-orientationandroid-orientation

android:webView handling orientation changes


I want to prevent webview reload the page everytime when orientation is changed.

I've followed following tutorial:

http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change

but it calls onCreate() method eveytime and fetches the page agin and again.

DisplayResult.java :

public class DisplayResult extends AppCompatActivity
{
protected FrameLayout webViewPlaceholder;


protected WebView webView;
protected Uri url;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_result);
    url = Uri.parse(getIntent().getStringExtra("Url"));
    initUI();
    Toast.makeText(DisplayResult.this ,"Oncreate",Toast.LENGTH_SHORT).show();
}


protected void initUI()
{
    webViewPlaceholder =(FrameLayout)findViewById(R.id.webViewPlaceholder);
    if(webView==null)
    {

        //webView = (WebView) findViewById(R.id.webview);
        webView = new WebView(this);
        // webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        //webView.getSettings().setTextZoom(0);
        webView.setWebViewClient(new Callback());
        webView.loadUrl(url.toString());

    }
    webViewPlaceholder.addView(webView);
}

private class Callback extends WebViewClient {
    MaterialDialog m ;

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

    @Override
    public void onPageStarted(WebView view, String url, Bitmap facIcon) {

        m=new MaterialDialog.Builder(DisplayResult.this)
                .title("Fetching Result")
                .content("Please wait...")
                .progress(true,0)

                .show();
    }
    @Override
    public void onPageFinished(WebView view, String url){
        m.cancel();
    }
}

@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){
    if(webView==null)
    {
        webViewPlaceholder.removeView(webView);
    }
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.display_result);
    url = Uri.parse(getIntent().getStringExtra("Url"));
    initUI();
}

}

and my layout file :

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
    android:id="@+id/webViewPlaceholder"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"></FrameLayout>

</LinearLayout>

I've also added in manifest file :

<activity
        android:name=".DisplayResult"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/display"
        android:parentActivityName=".MainActivity" />

Still it's not working. can anyone help me with solution :

I've also tried :

Android Webview Handling Orientation Not Working


Solution

  • @Override
    public void onConfigurationChanged(Configuration newConfig){
    if(webView==null)
    {
        webViewPlaceholder.removeView(webView);
    }
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.display_result);
    url = Uri.parse(getIntent().getStringExtra("Url"));
    initUI();
    }
    

    Replace this with

    @Override
    public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    }