Webview - 如何在Rotate上保留当前页面

时间:2017-12-20 09:50:47

标签: java android webview

我为我的企业开发了一个网站,我几乎只使用PHP工作

所以Java语言(和android studio)对我来说真的很新。尽管如此,我必须创建一个APK来使用该网站(为了阻止这个网站上的Android主页)

但是当我转动屏幕时,webview会显示主页而不是当前页面。

我做了一些研究,并整合了我在这里看到的内容:Android - Preventing WebView reload on Rotate

但这不起作用,我不明白为什么我总是回到“Google.com”

MainActivity.java

package eu.test.chrome;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    WebView webView;

    SwipeRefreshLayout swipe;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        LoadWeb();

        progressBar.setMax(100);
        progressBar.setProgress(1);


        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });


        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
            }
        });


        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }


            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                webView.loadUrl("file:///android_asset/error.html");
            }


            public void onPageFinished(WebView view, String url) {
                //Don't show ProgressBar
                progressBar.setVisibility(View.GONE);
                //Hide the SwipeRefreshLayout
                swipe.setRefreshing(false);
            }

        });

    }


    /* I integrated what I saw here: https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate but that does not work
    @Override
    protected void onSaveInstanceState(Bundle outState )
    {
        super.onSaveInstanceState(outState);
        webView.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        webView.restoreState(savedInstanceState);
    }*/


    public void LoadWeb() {

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.loadUrl("https://www.google.com/");
        swipe.setRefreshing(true);
    }

    @Override
    public void onBackPressed() {

        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rel_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/awv_progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="26dp"
        android:indeterminate="true" />


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>


</LinearLayout>

清单

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="eu.test.chrome">
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:hardwareAccelerated="false"
        android:configChanges="orientation|screenSize" 
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:2)

为了在旋转前加载Url,您需要添加一些内容。

private final String CACHE_URL_KEY = "CACHE_URL";

}

这些是制作的步骤:

  1. 您需要创建密钥以识别已保存的网址

    @Override protected void onSaveInstanceState(Bundle outState ) { outState.putString(CACHE_URL_KEY,webView.getUrl()); super.onSaveInstanceState(outState); }

  2. 每次轮播时都需要保存网址

    if (savedInstanceState != null) { String savedUrl = savedInstanceState.getString(CACHE_URL_KEY); webView.loadUrl(savedUrl); }else { webView.loadUrl("https://www.google.com/"); }

  3. 您需要检查是否存在旋转。如果是,则savedInstanceState不会为null,因此检索Url并加载它。 其他加载默认页面

    {{1}}

  4. 此外,将保存的状态传递给LoadWeb方法

答案 1 :(得分:1)

在Android中创建方法,如:

 @JavascriptInterface
 public boolean CallByMobApp(){
 return true;
}

JavaScript中创建调用功能:

$(window).load(function () {

IsCallByMobileApp = false;
try {
      IsCallByMobileApp = app.CallByMobApp();/*Is call by android app*/

} catch (e) {
    IsCallByMobileApp = false;
}
}

根据此方法,您可以管理要打开的页面。

答案 2 :(得分:1)

每次都调用LoadWeb方法。您需要检查savedInstanceState == null然后调用LoadWeb。如果savedInstanceState!= null,则不要调用LoadWeb

答案 3 :(得分:1)

试试这个

import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import android.support.v4.widget.SwipeRefreshLayout;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class Main2Activity extends AppCompatActivity {

    WebView webView;

    SwipeRefreshLayout swipe;
    ProgressBar progressBar;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);



        LoadWeb();
        progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
        progressBar.setMax(100);
        progressBar.setProgress(1);


        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {


                progressBar.setProgress(progress);
            }
        });

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                webView.loadUrl("https://stackoverflow.com/questions/47885592/how-to-add-a-progress-loading-bar-in-webview/47885692#47885692");
            }

            public void onLoadResource(WebView view, String url) { //Doesn't work
//                swipe.setRefreshing(true);
            }

            public void onPageFinished(WebView view, String url) {

                //Hide the SwipeReefreshLayout
                progressBar.setVisibility(View.GONE);
                swipe.setRefreshing(false);
            }

        });


    }

    public void LoadWeb() {

        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.loadUrl("http://dev.ncryptedprojects.com/bistrostays_v3/");
        swipe.setRefreshing(true);
    }

    @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 onBackPressed() {

        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

}

并在您的清单文件中

<activity android:name=".MainActivity"
   android:configChanges="orientation|screenSize">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />

       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>