当我更改屏幕方向时,如何控制android webview活动更改?

时间:2015-06-14 10:22:38

标签: java android android-studio android-webview screen-orientation

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);
    }


}

当我更改屏幕方向时,我的网页视图会自动进入主页。我想进行Web查看活动以防止在我更改屏幕方向时重新启动活动。请告诉我该怎么做。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:2)

使用savedInstanceState。仅在URL为空时加载URL。

@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);
}