Webview content flickering


This class has a fix in the setWebViewLayerTypeHardware method. If you remove the fix installation in onResume, where the SOFTWARE layer is installed, and then HARDWARE is installed after 1000 ms, then when you return to the screen of this class, the contents of the webview are displayed. It starts to flicker. How can I fix it without a delay of 1000 ms? If you change it immediately, the fix does not go away. I tried the same solutions with doOnLayout, preDraw, Choreographer - it also didn't help. And I have to use exactly HARDWARE layer

private val isWebViewClearFixEnabled

    get() = features?.getWebViewClearFix()?.isFeatureAvailable() == true



private val isFixEnabled

    get() = features.hasFeatureEnabled()





class CatalogFragment : BaseFragment() {



    private var catalogDelegate: CatalogDelegate? = null



    private var needsGpuReset = false



    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        catalogDelegate = ctalogDelegate(

            shouldCloseOnFinish = arguments?.getBoolean(KEY_CLOSE_ON_FINISH) ?: false,

            closeCatalog = { close() }

        )

        catalogDelegate?.restoreState(savedInstanceState)

        catalogDelegate?.onCreate(requireActivity())

        initMiniAppHandlers()

    }



    override fun onResume() {

        super.onResume()

        setWebViewLayerTypeHardware()

    }



    override fun onPause() {

        super.onPause()

        setWebViewLayerTypeSoftware()

    }



    override fun onSaveInstanceState(outState: Bundle) {

        super.onSaveInstanceState(outState)

        giftsCatalogDelegate?.saveState(outState)

    }



    override fun onDestroy() {

        catalogDelegate?.onDestroy(requireContext())

        super.onDestroy()

    }



    private fun setWebViewLayerTypeSoftware() {

        if (isFixEnabled) {

            val webView = browser?.state?.view

            webView?.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

            needsGpuReset = true

        }

    }



    private fun setWebViewLayerTypeHardware() {

        if (isFixEnabled && needsGpuReset) {

            needsGpuReset = false

            val webView = browser?.state?.view ?: return

            webView?.postDelayed({

                webView?.setLayerType(View.LAYER_TYPE_HARDWARE, null)

            }, 1000) // for weak devices, there is enough delay of 200 ms, for fast devices, it turned out empirically that it takes about 1000 ms.

        }

    }

    // other code

}
4
Feb 26 at 9:47 AM
User AvatarVladimir Fisher
#android#webview

No answer found for this question yet.