0

So I understand that when I rotate the screen the contents will be set back to how they were originally because the activity is created and destroyed for the new layout.

This is an issue for me as my web-app which I am displaying in a WebView displays unique information to the user, when they turn the phone sideways the data is lost. Now, I know the two methods I must override, but I'm not entirely sure of what I can put in the onSaveInstanceState to resurrect the exact page with the exact same data. I would have tried storing the URL and using that but the pages use random features so it would be different.

So far all I have is:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

Any thoughts of how to achieve this?

Jacob Ras
  • 5,974
  • 1
  • 28
  • 26
Tim
  • 1,056
  • 4
  • 17
  • 34

3 Answers3

2

It would probably be simpler if you just make your Activity not to restart during configuration changes by modifying its entry in the manifest to be similar to the following:

    <activity android:name=".MyActivity"
        android:configChanges="orientation|keyboardHidden" />

From the reference page for WebView:

The standard behavior for an Activity is to be destroyed and recreated when the device orientation or any other configuration changes. This will cause the WebView to reload the current page. If you don't want that, you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone. It'll automatically re-orient itself as appropriate.

UPDATE:

According to this answer, we should use

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

instead for device with Honeycomb and above version. Please give it a try :)

Community
  • 1
  • 1
Joe
  • 14,039
  • 2
  • 39
  • 49
  • Add a call to `Log.d("Tim", "onCreate called");` in your OnCreate() method to check if it is being called when you rotate the device. – Joe Aug 24 '12 at 17:25
  • It's definitely called! Could it be that I have the loadUrl method called in the onCreate so it simply loads the URL again? – Tim Aug 26 '12 at 14:08
  • No, the `configChanges` settings in the manifest are supposed to prevent the `onCreate()` of being called again. I updated my answer with additional information for Honeycomb+ devices. – Joe Aug 26 '12 at 14:23
1

try this work with me

    if (savedInstanceState == null)
{
  web.loadUrl(webURL);
}

add over method

@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}

link: Android - Preventing WebView reload on Rotate

Community
  • 1
  • 1
msmlz
  • 11
  • 1
0

As far as I understand, your problem lies on the web application, more than on the Android client implementation. If storing the URL is not enough, you will have to find another way to send these "random datas" to your host application, maybe via cookies, or javascript variables.

Orabîg
  • 11,718
  • 6
  • 38
  • 58