Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Webview doesn't load HTML sometimes

I am trying to load local HTML content in my webview. But sometimes it failed to load the content instead shows blank screen. It happens ones in every 5 times of loading.

NOTE My HTML content, I am trying to load is Official 2048 Source code.

Below is my Activity source code

public class GameActivity extends AppCompatActivity {

private WebView mWebView;

@SuppressWarnings("ConstantConditions")
@SuppressLint({ "SetJavaScriptEnabled", "NewApi", "ShowToast"})
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

    setContentView(R.layout.activity_game);

    Toolbar toolbar = (Toolbar) findViewById(R.id.game_toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    // Load webview with game
    mWebView = (WebView) findViewById(R.id.mainWebView);
    WebSettings settings = mWebView.getSettings();
    String packageName = getPackageName();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    if (Build.VERSION.SDK_INT >= 19) {
        // chromium, enable hardware acceleration
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        // older android version, disable hardware acceleration
        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    // Since the database path is automatically handled by Chromium Webkit,
    // we should not mention the db path for greater than KitKat version
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        settings.setDatabasePath("/data/data/" + packageName + "/databases");
    }
    mWebView.addJavascriptInterface(new WebInterface2048(this), "Android");
    // If there is a previous instance restore it in the webview
    if (savedInstanceState != null) {
        mWebView.restoreState(savedInstanceState);
    } else {
        mWebView.loadUrl("file:///android_asset/2048/index.html");
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

public class WebInterface2048 {
    Context mContext;

    public WebInterface2048(Context context) {
        mContext = context;
    }

    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
}

Till now I have tried the below things to solve the problem:

  1. Put Hardware Acceleration on the of Manifest file.
  2. Enabled and Disabled Hardware Acceleration inside Activity.
  3. Reloaded the same URL inside shouldOverrideUrlLoading
  4. Tried to load the URL inside onStart() instead of onCreate()

But nothing seems to be working for me.

My Logs:

D/OpenGLRenderer: endAllActiveAnimators on 0xb7d7e248 (RippleDrawable) with handle 0xb76b0cf0
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
I/cr_Ime: ImeThread is not enabled.
W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 18631
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
D/OpenGLRenderer: endAllActiveAnimators on 0xb7a893f8 (RippleDrawable) with handle 0xb7ec8810
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
I/cr_Ime: ImeThread is not enabled.
W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 18631
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow

Additional Info: My Device is Moto G3 (Android 6.0.1)

like image 972
Chandra Sekhar Avatar asked May 07 '16 15:05

Chandra Sekhar


2 Answers

Never understood why this is happening. But as a workaround, I came out of this problem using below code:

mWebView.postDelayed(new Runnable() {

        @Override
        public void run() {
            mWebView.loadUrl("file:///android_asset/2048/index.html");
        }
    }, 500);

Instead of loading the URL in main thread, if I am running in a different thread, it worked for me.

The above work around can be helpful for those, who are facing problem, where their webview shows blank when loads first time and on reloading, it loads the content.

May be its not a prob=per solution, but it worked. If anybody is finding a better solution, please do post.

like image 146
Chandra Sekhar Avatar answered Sep 28 '22 11:09

Chandra Sekhar


I solved the issue by manual adding WebView into the layout within onCreate method:

        LinearLayout ll = findViewById(R.id.content);
        WebView help = new WebView(this);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        help.setLayoutParams(lp);
        ll.addView(help);

Then load html as usual:

        try {
             String base64 = Base64.encodeToString(sampleHtml.getBytes("UTF-8"), Base64.DEFAULT);
             help.loadData(base64, "text/html; charset=utf-8", "base64");
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         } catch (Exception ex){
             ex.printStackTrace();
         }
like image 28
Vadym Vikulin Avatar answered Sep 28 '22 11:09

Vadym Vikulin