Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflating WebView is slow since Lollipop

In fact I am surprised that I found no similar question in Stackoverflow, but I really can't figure it out.

First of all, please be reminded that this is not related to the performance of browsing webpage inside a WebView. It is only about inflating even an empty WebView, without loading any URL.

To eliminate all other factors, I have created a brand new project which contains things only necessary to reproduce the problem.

In this almost empty project, I have a layout which contains only a WebView:

<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.testing.WebViewOnlyActivity">

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

</FrameLayout>

And I have a MainActivity which contains only a Button which starts this WebViewOnlyActivity:

public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, WebViewOnlyActivity.class);
                startActivity(intent);
            }
        });
    }
}

Simple enough, right?

Now, when I build the project, click the button, the WebViewOnlyActivity takes more than 1 second to popup. But then if I press back button, and press the button again, the WebViewOnlyActivity appears immediately. Apparently Android cached something of WebView.

Here are my testing devices:
1. Android 7.1.1: Slow
2. Android 7.0.0: Slow
3. Android 5.0.2 Slow
4. Android 4.4.1 Fast

So it appears to me this only happens since Lollipop.

However, such 1.x second delay is still unacceptable to me. Does anyone knows how to solve it? (Well, I think it is hard because I did nothing to this WebView... May be I should use some 3rd party WebView?)

P.S. I have also used traceview to see what are the differences between a 7.1.1 device and 4.4.1 Device. It is clear that WebView's init() method is taking up a lot of time.

7.1.1: 7.1.1 Trace View And further deeper: 7.1.1 Deeper

4.4.1: 4.4.1 Trace View And further deeper: 4.4.1 Deeper

Big Difference!


Although it is really empty, but requested by Axel, I will post the code for WebViewOnlyActivity as well:

public class WebViewOnlyActivity extends AppCompatActivity {

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

    }
}

like image 252
Sira Lam Avatar asked Oct 25 '17 08:10

Sira Lam


1 Answers

EDIT 2017-12-21

I even created a library to tackle this problem easily.
You may download it here.
Give it a star if you find it useful :P


Thanks to @XavierRubioJansana 's hint, I tried ViewStub to delay the inflation.

However, even if I delay the inflation to onResume(), it will still slows the activity creation.

But then if I use a Handler to postDelay the inflation, activity creates immediately!

    Handler webViewInflateHandler = new Handler();
    webViewInflateHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (!webViewInflated) {
                webView = (WebView) webViewStub.inflate();
                webViewInflated = true;
            }
        }
    }, 50);

Then, you need to delay all kinds of setup and loading of WebView until it is inflated. I will suggest using RxJava here to observe an BehaviorSubject<Boolean>.

like image 140
Sira Lam Avatar answered Nov 16 '22 17:11

Sira Lam