使用Android,当我使用Web视图中的后退键时,会出现白屏

时间:2017-09-14 16:45:10

标签: android webview

我正在使用一个显示WebView的非常简单的活动,但是当我从Web视图返回上一个活动时,会出现一个白色屏幕,我需要再按一次后退键才能成功。有办法防止这种情况吗?

网络活动Display_web.java:

<link rel = "stylesheet" type = "text/css" href = "FILENAME.EXTENSION">

xml文件display_web.xml:

public class Display_web extends Activity {
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String refOeuvre = intent.getStringExtra("REFOEUVRE");
        setContentView(R.layout.display_web);

        myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDomStorageEnabled(true);
        myWebView.loadUrl(refOeuvre);
    }

    @Override
    public void onBackPressed() {
        if(myWebView!=null && myWebView.canGoBack())
            myWebView.goBack();// if there is previous page open it
        else
            super.onBackPressed();//if there is no previous page, close app
    }
}

我使用WebView和ListView调用新WebView的活动:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

0 个答案:

没有答案