Prevent WebView reloads in FragmentPagerAdapter?

后端 未结 1 840
情歌与酒
情歌与酒 2021-02-03 11:57

I have a FragmentPagerAdapter used to show about 6 tabs, all of which load their data from a web server. One of the tabs contains a WebView that loads an image from my server. <

1条回答
  •  时光取名叫无心
    2021-02-03 12:35

    The problem appears to be that you are wiping out your own results.

    While you need a new WebView on a configuration change, you do not need a new WebView otherwise. And, if you already have the WebView, you do not have to tell it to do anything.

    So, I'd try this:

    • Hold onto the WebView that you create in onCreateView() in a data member of the fragment
    • Only inflate the layout in onCreateView() if that data member is null
    • Only call loadUrl() if you inflated the layout

    (if you are creating the WebView in Java code, replace "inflate the layout" with "create the WebView via its constructor")

    If the contents of the fragment is more than the WebView, you will also need to hold onto the root view that you inflated in a data member, so you can return that from onCreateView().

    0 讨论(0)
提交回复
热议问题