简体   繁体   中英

How do I prevent the reload of an Android WebView during an orientation change (nested fragments)?

I know there are a lot of questions like this that have been asked but I seem to have a specific situation that hasn't been covered in any of the duplicate questions.

So I'll start by informing you of my situation. I have an Activity which holds a fragment which holds another fragment (WebViewFragment). So I have a nested fragment. The WebView is a VideoEnabledWebView . I'm using this because I have videos inside the html5 inside the WebView which I want to be able to show in fullscreen. The problem is: when you have a video in fullscreen, and then change the orientation of the device, the Activity is restarted and so the WebView reloads causing the video to close. I then have to restart the video from the beginning to play it in the new orientation. I want to be able to change the orientation of the device without interrupting the video. So the video stays fullscreen and continues where it left off.

According to the Android documentation, there are two ways to go about dealing with an orientation change:

  1. Handling the Configuration Change Yourself

This is the most popular solution to this question on stack overflow. In which I plug android:configChanges="keyboardHidden|orientation|screenSize" into my activity in the manifest. This works but I can't use it because it prevents the entire Activity from restarting. Therefore, if I'm in portrait mode, my landscape specific layout xml files don't get loaded. Maybe there is a way to reload the layout files? But this all sounds hacky and I'm not sure what other stuff I need to take care of manually which is usually done automatically by the Android OS during a config change.

  1. Retain an object during a configuration change.

This is not possible for me because you have to use setRetainInstance(true) and you can't do this with child fragments. But, does anyone know if this will work if I call setRetainInstance(true) on the parent fragment? I'm currently testing this but it is taking me a few hours to implement (since I'm kinda new to android). Before I waste anymore time attempting a possibly bad solution, can anyone verify whether or not this will work? In the meantime, I will continue to work on this and let you know if it does work when I'm done.

Thanks!

Add the "screenSize" attribute in AndroidManifest.xml file:

<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize">

Hope it will fix your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM