7

我有一个带有 EditText 和一个 imageview 和一个按钮的小活动。当您按下按钮时,它会启动相机以获得结果,当它返回时,它将图像视图更改为您刚刚拍摄的照片。

但是当方向改变时,图像视图会重置回布局上的默认值。

我尝试做的是设置一个名为自定义的布尔值,当你拍照时它设置为真。我覆盖 onConfigurationChanged() 并且如果 custom 设置为 true 我恢复图像。

我现在的问题是 EditText 被删除了——如何在配置更改后恢复 EditText?我的第一次尝试是将它的内容存储到 String onPause() 中,然后恢复它,但它总是空白。

4

2 回答 2

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

于 2011-08-17T07:21:27.810 回答
6

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

于 2011-08-17T06:49:33.217 回答