My views are being reset on orientation change
W

2

7

I have a small activity with an EditText and an imageview and a button. When you press the button it launches camera for result, and when it returns it changes the imageview to the picture you've just taken.

But when the orientation changes, the imageview resets back to the default one on the layout.

What I tried doing was setting a boolean called custom, and when you take a picture it sets it to true. I overrid onConfigurationChanged() and if custom is set to true I restore the image.

My problem now is the EditText becomes erased -- How can I restore the EditText after configuration change? My first attempt was storing it's content into a String onPause() and then restoring it, but it always comes up blank.

Weep answered 17/8, 2011 at 6:40 Comment(0)
T
16

Usually when UI view does not keep its state, first thing to check is that this UI view has id assigned. Without this id views cannot restore their state.

 <EditText android:id="@+id/text" ... />

If this doesn't help, you need to save and restore state yourself. Take a look at Handling Runtime Changes. It pretty much explains what you should do:

To properly handle a restart, it is important that your Activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your Activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreInstanceState(). To test that your application restarts itself with the application state intact, you should invoke configuration changes (such as changing the screen orientation) while performing various tasks in your application.

You should override onSaveInstanceState() and save your Acitivity state when its called:

@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    outState.putString("textKey", mEditText.getText().toString());
}

And then restore state in onCreate() or onRestoreInstanceState():

public void onCreate(Bundle savedInstanceState)
{
    if(savedInstanceState != null)
    {
        mEditText.setText(savedInstanceState.getString("textKey"));
    }
}

If this is still not sufficient, you can override onRetainNonConfigurationInstance() and return any custom Object that will be passed to new activity object, when its recreated. More details about how to use it can be found in Handling Runtime Changes. But this function is deprecated in Android 3.0+ (specifically for FragmentActivity where its final). So this cannot be used together with Fragments (which is fine, they have their mechanism to retain objects accross configuration changes).


And final one - never use android:configChanges. You must have very good reasons to use it, and usually these are performance reasons. It wasn't meant to be abused the way it is now: just to prevent UI state reset. If this attribute is used, then yes, Activity UI will not be re-set on config change, but Activity state still will be reset when destroyed and re-created later.

The documentation explains this option pretty well:

Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort and is not recommended for most applications

Tremulant answered 17/8, 2011 at 7:21 Comment(2)
Okay so that sounds like a much better approach. I can use this for the bitmap instead, and then the edittext won't be a problem -- But which function would I use for saving the image's state?Weep
If the bitmap is saved on disk, then just save path to bitmap in onSaveInstanceState() function. If bitmap is in memory, then you can use onRetainNonConfigurationInstance().Tremulant
M
6

You can force your activity not to reload after orientation changed by adding android:configChanges="orientation" to your activity line in manifest.

Mazzola answered 17/8, 2011 at 6:49 Comment(4)
But wouldn't that make the activity not adjust to the phone being sideways?Weep
This is the correct answer. The one that prevents the screen to change orientation is android:screenOrientation="portrait"Muskeg
While this fixes this particular case, its doesn't fix the underlying issue. And android:configChanges is considered to be last resort, and should normally never be used. developer.android.com/guide/topics/resources/…Tremulant
just because it's complex to handling config changes by oneself doesn't mean it should not be used, if someone has guts and knows inside out (knows what he's stepping into) then for sure go for it.Topper

© 2022 - 2024 — McMap. All rights reserved.