0

I have developed an application and it contains web view.

My issue

When my phone's orientation changes from portrait to landscape the whole application loads again and the web view reloads showing the first page of website.

So I am getting confused about the screen orientation or saving the data during that phase, so how do I fix it...

cafebabe1991
  • 4,416
  • 1
  • 25
  • 36

3 Answers3

0

The default behaviour is to restart the activity when a configuration change happens (such as orientation change).

To override this you need to tell the system you'll handle orientation change yourself by adding this to your manifest file in your <activity> element:

android:configChanges="keyboardHidden|orientation|screenSize"

You may also want to override onConfigurationChanged which will be called when such a change happens.

See http://developer.android.com/guide/topics/manifest/activity-element.html#config

sdabet
  • 17,379
  • 11
  • 73
  • 143
0

Yes, in Android the Activity is destroyed and recreated when your change the screen orientation. Thus you need to restore your applications state on recreation. On way is to use the callback method onSaveInstanceState() which you can use to save the state in a Bundle.

The Activities are explained here: http://developer.android.com/guide/components/activities.html and I suggest you take a look on that page for examples and more detailed instructions.

TupeT
  • 26
  • 3
0

The Android Activity lifecycle clearly indicates this behaviour .

What happens

Whenever you start an Activty is gets created(after onStart() method) the

onCreate(Bundle onSavedInstance)

The variable onSavedInstance mentioned above initially recieves null as nothing is saved. But during the orientation the whole layout hierarchy has to adjust according to the new mode from an existing mode(from portrait->landscape or vice-versa).This change may remove the previous data that you had in your activity.So to avoid such a problem(loosing data), there is a method

onConfigurationChange(Bundle saveSomething)

This method will be called for you to handle some configuration changes into this, The default implementation will save you some data like some text in an editText.

Note This method as far the specs goes should be used to save some trivial data.

Example Suppose you had applied a background colour to the activty layout and now you rotated it default implementation won't save it but onConfigurationChange if you want you can save it like this

saveSomething.putInt("color",1);

Inside onCreate 

    protected void onCreate(Bundle onSavedInstance){

        if(onSavedInstance!=null){
           int color=onSavedInstance.getInt("color");
           if(color==1){
             setBackgroundColor(Color.BLACK);
           }
        }
}

Add the following line inside the activity element of your manifest file you will be handling the changes in configuration

android:configChanges="keyboardHidden|orientation|screenSize"
cafebabe1991
  • 4,416
  • 1
  • 25
  • 36