Search code examples
androidandroid-webviewwebviewclient

Android - Best Practice for using a common WebView client


I'm creating an app where there are WebViews in many different activities. But all the WebViews use the same WebViewClient. Currently I am creating a new WebViewClient for every WebView and using it. I wanted to know whats the best way of doing this. Is it a good idea to put a static final WebViewClient object at the Application level and use it for each WebView or is there any other better/right way of doing it?


Solution

  • I would not make a static WebViewClient as all your activities' WebViews need their own instantiation of the object and making it static could cause problems.

    What you're doing is fine. However, if all of your WebViewClient definitions do exactly the same thing, you may want to think about defining it once in a separate class by extending WebViewClient and overriding the methods that you need. Then assing your custom webViewClient to your webviews in each of your activities.