-2

I have an app that adds to a total variable when a button is clicked. However if I turn my phone so the layout gets turned into the horizontal layout the values all get reset, and idea why this is and how to stop it?

Thanks!

Pete
  • 27
  • 2

3 Answers3

3

When you move your device, your device's Orientation State changes from Portrait to Landscape of from Lanscape to Portrait. In this Orientation change, your Activity's onCreate Method is called every time. Therefore the values in your Activity are being reset.

There are 2 ways of solving this problem:
1) Let You Activity manage it for you.
2) Managing the changes yourself by saving and restoring states.

Using 1st way of solving this problem:

Just add this line in your Activity Node in your Manifest.xml file.

android:configChanges="orientation|keyboardHidden|screenSize"

For example:

<activity
android:name=".MyMainActivity"
android:configChanges="orientation|keyboardHidden|screenSize" 
android:label="@string/app_name" >

Using the second Way:
You can override these two Methods:

public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
//Manage your Data Explicitly here.
}

public void onRestoreInstanceState(Bundle bundle) {
super.onRestoreInstanceState(bundle);
//Manage your Data Explicitly here.
}

EDIT
According to Android Dev Guide:
Using android:configChanges="orientation|keyboardHidden|screenSize" is not a good practice.

Quote from this page
Note: Using this attribute should be avoided and used only as a last resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

I recommend you to follow the Android Dev Guide for Handling Run-time Changes and follow the good Practices.

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • masking a problem is not solving it. You should be properly saving state and restoring it on rotations. – natez0r Aug 08 '13 at 23:33
  • It is not Masking approach, It is an implicit way to tell the Android OS to manage the Orientation Changes. In your terms 'Properly Saving' is the explicit way. and in the current scenario there is no such Data that is dependent of some external intent and that should be saved explicitly using the onRestore and onSaved. – Salman Khakwani Aug 08 '13 at 23:38
  • @natez0r this isn't necessarily "making a problem". I think Salman should give some more explanation with the answer and maybe note the warnings about it. But I handle config changes myself and haven't had any problems thus far. – codeMagic Aug 08 '13 at 23:38
  • Now that you've edited your answer, it at least provides the alternatives. However, swallowing configChanges does not mean that "Android OS manages it for you", it means you are managing it yourself because you know what's best. You will still see this problem if the activity goes in the background and gets killed by the OS due to memory constraints. Your state will be lost. See: http://stackoverflow.com/a/7990543/395176 – natez0r Aug 08 '13 at 23:53
  • Salman - Thanks for updating your answer. I think this now tells the whole story for the asker, they can now decide what's right in their scenario. – natez0r Aug 09 '13 at 00:20
  • 1
    Super Correct answer, super easy solution, thanks dude, you answer helped me too! – AAlferez Feb 21 '14 at 19:31
1

you need to save the variable in OnSaveInstanceState and restore it in onRestoreInstnace state

For example;

 @Override
public void onSaveInstanceState(Bundle bundle) {
    bundle.putParcelableArrayList(PEOPLE, people);
            super.onSaveInstanceState(bundle);  

}

@Override
public void onRestoreInstanceState(Bundle bundle) {
    super.onRestoreInstanceState(bundle);
    people = bundle.getParcelableArrayList(PEOPLE);

}
nPn
  • 16,254
  • 9
  • 35
  • 58
1

This happens because the normal way Android handles an activity during any configuration change (including screen reorientations) is to destroy the activity and recreate it. As described in the guide topic Handling Runtime Changes, you can handle it a couple of ways. The "Android way" is to save your activity's state information by overriding onSaveInstanceState and onRestoreInstanceState methods. The details about how to use these methods can be found in the guide topic Recreating an Activity.

The other way to prevent this problem is to tell Android that your activity will handle configuration changes internally. You do this by adding android:configChanges="orientation" to the <activity> tag in the manifest for the activity and overriding the onConfigurationChanged method of the activity to actually handle the changes.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521