1

In my activity, I am loading you-tube video content into my web view like this:

webview.loadData("<iframe class=\"youtube-player\" " +
"type=\"text/html\" "+" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\">", "text/html", "UTF-8");

which is loading you tube video. Upon clicking play button video is getting played. All this is working fine.

When I rotate the screen How can I restore the video state in web view means if video is played for 30 secs before rotation. I have to start from 31 sec after rotation. Applying

webview.saveState(bundle);
webview.restoreState(bundle); 

in onSave and onRestore life cycle methods doesn't work for me.

Ganesh K
  • 2,623
  • 9
  • 51
  • 78

3 Answers3

0

you want to restore the video from the location you were earlier. suppose you have called onPause() method. overwrite the onpause method and load the value of the int a = video.currentlocation() in a variable and later overwrite the onresume method , vide.seekto(a). video.start();

Pranav Sharma
  • 692
  • 2
  • 9
  • 22
0

You can create WebView programmatically and not to define it into your Layout file.

Instead of this create a kind of ViewGroup in your layout with ID, and after creation of WebView in activity just add it into this ViewGroup.

Save instance of WebView in onSaveInstanceState(Bundle outState) method by

webView.saveState(outState);

and restore it in onCreate(Bundle savedInstanceState) method by

webView.restoreState(savedInstanceState);

Restore state only if savedInstanceState is not null; otherwise configure your webView. If the state of the webView is restoring, there's no need to configure it again.

Big thanks to https://twigstechtips.blogspot.ru/2013/08/android-retain-instance-of-webview.html

tikhonos
  • 592
  • 4
  • 11
-1

So far the best solution I found is - placing web view in Frame Layout and handling orientation changes by ourself. On Configuration changes, removing web view from Frame Layout and resetting main view after that adding web view to Frame Layout. link for working sample

Ganesh K
  • 2,623
  • 9
  • 51
  • 78