WebView: how to preserve the user's zoom settings across sessions?

前端 未结 3 665
我在风中等你
我在风中等你 2020-12-10 08:45

My app uses a WebView, and users sometimes adjust the zoom level to make the text larger. However the zoom level setting is lost when the Activity is closed and another one

3条回答
  •  -上瘾入骨i
    2020-12-10 09:29

    The answer Jorge gave is spot on, except that SaveZoom ideally should not be called on finish() (which is when the activity is destroyed). A better zoom preference saving approach would be to extend the WebView's WebViewClient, and override onScaleChanged(). So continuing Jorge's onCreate method:

    public void onCreate(Bundle savedInstanceState)
    {
    
        ....
    
        class MyWebViewClient extends WebViewClient
        {
            @Override
            public void onScaleChanged(WebView wv, float oldScale, float newScale)
            {
                SaveZoom();
            }
        }
    
        mWebView.setWebViewClient(new MyWebViewClient());
    }
    

提交回复
热议问题