1

I see my WebView is being reloaded in many different cases: after changing orientation, after turning off/on etc.

Is there a simple way to stop reloading WebView at all? It's kinda weird, when a component does such important things on its own, instead of throwing NeedReload event or something.

Regards,

UPDATE

To beat turning screen off as well as orientation handling, do this:

  1. Create the view in Application.onCreate().
  2. Add the view to a layout in Activity.onCreate().
  3. Remove the view from the layout in Activity.onDestroy().

Details are here: Attach/detach Android view to/from layout

Community
  • 1
  • 1
noober
  • 4,819
  • 12
  • 49
  • 85

1 Answers1

3

Webview content will be loaded depending on where you called your loadurl . For ex: if you written your loadurl function in onCreate() then that function will executes when orientation changes. you can restrict them in manifest file android:configChanges="orientation|keyboardHidden" . So be sure where to call your load url function and also look at Activity life cycle

vnshetty
  • 20,051
  • 23
  • 64
  • 102
  • But what about turning device on/off? And possible other scenarios? – noober May 03 '12 at 06:49
  • @noober while device ON (off! anyway its going to off who cares if it reloads) it probably executes oncreate() i think .. pls refer activity life cycle and override proper methods.. and some code would be lot better to understand your problem.. so post your code... – vnshetty May 03 '12 at 06:54
  • My code is very trivial. I load a page in onCreate(), that's it. Since my app is a web-app, all other code is HTML/JS/CSS. I do not want the webview be reloaded, because if I turn the device off then on playing AngryBirds, the current level is never restarted from scratch. If I do the same with my app, any unsaved progress is lost. Is it because the view is being destroyed every time and onCreate() is being called after that? – noober May 03 '12 at 08:54
  • yes..But to avoid those orientation change problem you can use android:configChanges="orientation|keyboardHidden" for your activity in manifest file. – vnshetty May 03 '12 at 09:04
  • Thank you. So, the right question is: how to prevent destroying the webview when device is off (on lost focus?), isn't it? – noober May 03 '12 at 09:08
  • device off means power off or just a screen lock?. if it power off then while power on it will call onCreate() method but if it just a screen lock then it calls onPause() may be onStop() etc.. and while unLock it will call onResume() but it wont call onCreate()... And one more thing if you dont add the android:configChanges="orientation|keyboardHidden" stement in manifest file then onCreate method will be called for every orientation change – vnshetty May 03 '12 at 09:14
  • It's just screen lock, since a sound file (audio HTML5 tag) keeps playing. Then user presses the power button again and -- oops! -- the page is reloaded, music is restarted, progress is lost. Well, I see now, the view is not destroyed then, yes? So it's reloaded from onResume(), maybe... Have to setup breakpoints and examine. – noober May 03 '12 at 09:19
  • That's why I looked for a way to prevent the view from reloading at all... still can't believe the OS engineers are so unfriendly to web-developers. It's just a web-app, why should I write so many platform code... – noober May 03 '12 at 09:25