WebViewFragment webView is null after doing a FragmentTransaction

前端 未结 4 1864
故里飘歌
故里飘歌 2020-12-24 15:18

I currently have my application set up with a ListFragment on the left and a DetailsFragment on the right (similar to the layout on the tablet belo

4条回答
  •  醉酒成梦
    2020-12-24 16:00

    WebViewFragment as is is not that straightforward to use. Try this simple extension (You can copy/paste):

    public class UrlWebViewFragment extends WebViewFragment{
    
        private String url;
    
        public static UrlWebViewFragment newInstance(String url) {
            UrlWebViewFragment fragment = new UrlWebViewFragment();
            fragment.url = url;
            return fragment;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            WebView webView = (WebView) super.onCreateView(inflater, container, savedInstanceState);
            webView.loadUrl(url);
            return webView;
        }         
      }
    

    Call where you need using the factory method:

    WebViewFragment fragment = UrlWebViewFragment.newInstance("http://ur-url.com");
    

提交回复
热议问题