How can I control android webview activity changes when I change screen orientation?

881 views Asked by At
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.gc.materialdesign.views.ProgressBarDeterminate;

import java.util.logging.Handler;

import javax.security.auth.Destroyable;

/**
 * Created by myozawoo on 4/9/15.
 */
public class HomeActivity extends ActionBarActivity{


    WebView wv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_webview);

            wv = (WebView) findViewById(R.id.myWeb);
            wv.getSettings().setJavaScriptEnabled(true);
            wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            wv.getSettings().setDomStorageEnabled(true);
            wv.setWebViewClient(new MyWebViewClient());
            wv.setWebChromeClient(new MyWebChromeClient());

            wv.loadUrl("http://www.mmcloudschooling.com");


    }



    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);

        wv.restoreState(savedInstanceState);
    }




        @Override
    public void onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack();
        } else {
            finish();
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            ProgressBarDeterminate pb = (ProgressBarDeterminate) findViewById(R.id.progress);
            pb.setVisibility(ProgressBarDeterminate.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            ProgressBarDeterminate pb = (ProgressBarDeterminate) findViewById(R.id.progress);
            pb.setVisibility(ProgressBarDeterminate.GONE);
        }
    }

    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            ProgressBarDeterminate pb = (ProgressBarDeterminate) findViewById(R.id.progress);
            pb.setProgress(newProgress);
            super.onProgressChanged(view, newProgress);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            wv.reload();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

When I made screen orientation change, my web view reach to home page automatically. I wanna make my web view activity to prevent restart activity when I made screen orientation change. Please, tell me how to do. Sorry for my poor English.

1

There are 1 answers

3
Bidhan On BEST ANSWER

Use savedInstanceState. Load the url only when it is null.

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        wv = (WebView) findViewById(R.id.myWeb);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wv.getSettings().setDomStorageEnabled(true);
        wv.setWebViewClient(new MyWebViewClient());
        wv.setWebChromeClient(new MyWebChromeClient());


        if (savedInstanceState == null)
        {
           wv.loadUrl("http://www.mmcloudschooling.com");
        } else {
           wv.restoreState(savedInstanceState);

        }


}

@Override
protected void onSaveInstanceState(Bundle outState )
{
    super.onSaveInstanceState(outState);
    wv.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);
    wv.restoreState(savedInstanceState);
}