Apperside
Apperside

Reputation: 3602

android webview do not load in fragment

I'm facing with a very very strange problem: I'm trying to load a webview in a fragment, but I sware I can't find a way to make it working and I have absolutely any idea about the reason. The strange is that the same identical code works if run in an Activity

this is my very basic layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <WebView
        android:id="@+id/webview"
        android:background="@color/black"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

this is the activity where the code works

public class ProvaWebview extends Activity {
    WebView wv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prova_webview);

         wv=(WebView)findViewById(R.id.webview);
        WebSettings settings=wv.getSettings();
        wv.setWebChromeClient(new WebChromeClient() {
        });
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = getHTML();
        settings.setJavaScriptEnabled(true);
        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
       wv.clearHistory();
       wv.clearCache(true);
       wv.loadUrl("about:blank");
       wv.pauseTimers(); //new code
       wv = null;
    }

    public String getHTML() {
        String html = "<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/hEFqVV5q44E?rel=0&amp;controls=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>";
        return html;
    }



}

and this is the fragment where the code don't work

public class FrTabDescriptionWebView extends Fragment {
    public static final String TAG = FrTabDescriptionWebView.class.getName();


    private WebView wv;


    public static FrTabDescriptionWebView newInstance() {
        FrTabDescriptionWebView fragment = new FrTabDescriptionWebView();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    public FrTabDescriptionWebView() {
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.activity_prova_webview, null);

    }




    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        wv = (WebView) view.findViewById(R.id.webview);
        WebSettings settings = wv.getSettings();
        wv.setWebChromeClient(new WebChromeClient() {
        });
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = getHTML();
        settings.setJavaScriptEnabled(true);
        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

    }

    public String getHTML() {
        String html = "<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/hEFqVV5q44E?rel=0&amp;controls=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>";
        return html;
    }


}

what the hell am I doing wrong??

I also tryied to use a WebViewFragment replacing wv with getWebView but the problem still remain

thankyou for your help

Upvotes: 0

Views: 2330

Answers (2)

Apperside
Apperside

Reputation: 3602

I found the problem:

I had another Activity showing a WebView embedding a youtube video, and to make it stopping when closing the Activity I was using this code in the finish method

       wv.clearHistory();
       wv.clearCache(true);
       wv.loadUrl("about:blank");
       wv.pauseTimers();
       wv = null;

avoiding this code made the fragment working. I think that the real code causing the problem was

 wv.pauseTimers();

CONFIRMATION

After some testing I can confirm that the real problem was

wv.pauseTimers();

removing this line everything works as expected, to make a youtube video stop playing this code works fine

wv.clearHistory();
           wv.clearCache(true);
           wv.loadUrl("about:blank");
           wv = null;

Upvotes: 1

M D
M D

Reputation: 47817

Move your all code from onCreated(..) to onCreateView(...). and also remove

super.onCreateView(inflater, container, savedInstanceState);

Corrected:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.activity_prova_webview, null);

    wv = (WebView) view.findViewById(R.id.webview);
    WebSettings settings = wv.getSettings();
    wv.setWebChromeClient(new WebChromeClient() {
    });
    final String mimeType = "text/html";
    final String encoding = "UTF-8";
    String html = getHTML();
    settings.setJavaScriptEnabled(true);
    wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

   return view;

}

Output:

enter image description here

Upvotes: 3

Related Questions