简体   繁体   English

Android Webview恢复后不显示html文件

[英]Android Webview not showing html file after resume

i have a webview that read HTML from SD-card and show it . 我有一个Web视图,可从SD卡读取HTML并显示它。 when user open activity for first time, HTML file load correctly . 用户首次打开活动时,HTML文件将正确加载。 but in some devices (such as Samsung galaxy series) if close activity and open it again, webview not show anything and is empty. 但是在某些设备(例如Samsung galaxy series)中,如果关闭活动并再次打开它,则webview不会显示任何内容,并且为空。

public class page extends Activity {

private static WebView web;
private  int path;

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

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras(); 

    path = bundle.getInt("path");

    web = (WebView) findViewById(R.id.webview);
    web.setBackgroundColor(Color.TRANSPARENT);
    web.getSettings().setJavaScriptEnabled(true);
    web.setWebChromeClient(new WebChromeClient());

}

@Override
protected void onResume() {
    super.onResume();
    loadPage();

}

@Override
protected void onPause() {
    super.onPause();
    resetWebView();
} 

// becuase my webview play audio and if close activity sound also playing
private void resetWebView(){
    if (web != null){
        web.loadUrl("about:blank");
        web.destroy();
        web.destroyDrawingCache();
    }
}

private void loadPage(){

    String DIR =  Environment.getExternalStorageDirectory().getAbsolutePath()+"/myApp";

    if(path != null && web != null){
   switch (path) {
    case 3: {
        //   something like this 
        //  /storage/emulated/0/myApp/1/page_three.html/
        File file = new File(DIR+path+"page_three.html/");
        web.loadUrl("file://"+file);

    }
        break;
    case 2: {
        File file = new File(G.DIR_APP+path+"page_two.html/");
        web.loadUrl("file://"+file);
    }
        break;
    case 1: {
        File file = new File(G.DIR_APP+path+"page_one.html/");
        web.loadUrl("file://"+file);
    }
        break;
    case 0: {
        File file = new File(G.DIR_APP+path+"welcome.html/");
        web.loadUrl("file://"+file);
    }
        break;

    default:
        // do nothing
       }
   }
}

I had the same issue. 我遇到过同样的问题。 This is for API 11 and greater. 这适用于API 11及更高版本。 Adding webview.onResume() inside the onResume() function of fragment solved it for me. 在片段的onResume()函数内添加webview.onResume()可为我解决此问题。

So in your case: 因此,在您的情况下:

public void onResume() {
    super.onResume();
    web.onResume();
}

If the activity is destroyed while paused, the Android system may re-create it when the user returns to it later. 如果活动在暂停时被销毁,则Android系统可能会在用户稍后返回时重新创建。 To ensure that the same data that was originally used to intialise the activity is restored, you need to override onSaveInstanceState() and onRestoreInstanceState() . 为了确保恢复与最初用于初始化活动相同的数据,您需要重写onSaveInstanceState()onRestoreInstanceState() I recommend this pattern - this will ensure the correct data is loaded for initial creation, resume after pause, recreation and resume after being destroyed, and when sent a new intent to load new data. 我建议使用此模式-这将确保加载正确的数据以进行初始创建,暂停后恢复,重新创建并销毁后恢复以及在发送新意图以加载新数据时恢复。

static final String ARG_PATH = "path";
protected Bundle bundle = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bundle = getIntent().getExtras();
// perform other create-time initialisation here
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    bundle = intent.getExtras();
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    bundle = savedInstanceState;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(ARG_PATH, path);
}

@Override
protected void onResume() {
    super.onResume();
    if(bundle != null) {
        path = bundle.getInt(ARG_PATH);
        bundle = null;
        loadPage();
    } else
        web.onResume();
    // if bundle was already null, we were just resumed after pause, not destroyed so no reload is required.
}

@Override
protected void onPause() {
    web.onPause();    // pause any video etc.
    super.onPause();
}

当您离开活动时,请完成活动,因此,下次访问时,必须再次调用onCreate(Bundle savedInstanceState)方法。

我将以下代码添加到webview中并解决了问题

      webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM