我的观点正在被复位方向变化 [英] My views are being reset on orientation change

查看:135
本文介绍了我的观点正在被复位方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText和一个ImageView的和一个按钮的小活动。当你preSS的按钮,它启动相机的结果,当它返回它改变了ImageView的到你刚刚拍摄的照片。

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.

但是,当方向发生变化,ImageView的重置回布局上的默认值。

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

我想这样做是设置了一个名为自定义布尔,当你拍摄一张照片将其设置为true。我overrid onConfigurationChanged(),如果自定义设置为true,我恢复的形象。

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.

我现在的问题是EditText上变得擦除 - 配置更改后,我怎么能恢复的EditText?我的第一次尝试将其存储的内容转换成一个字符串的onPause(),然后还原它,但它永远是空白。

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.

推荐答案

通常,当用户界面视图不会保持它的状态,要检查的第一件事是,这个UI视图已分配的ID。如果没有这个ID的观点不能恢复自己的状态。

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" ... />


如果这没有帮助,你需要保存和恢复状态的自己。看看处理运行时更改。它pretty的多解释了什么是你应该做的:


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:

要正确处理重新启动,这是非常重要的,你的活动通过正常的活动周期,其中Android的的onSaveInstanceState()之前,它要求恢复其previous状态破坏你的活动,这样可以节省大约应用程序状态的数据。然后,您可以在的onCreate恢复状态() onRestoreInstanceState()。要测试您的应用程序与应用程序的状态重新启动本身完好无损,应该调用配置更改(例如更改屏幕方向),而在应用程序中执行各种任务。

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.

您应该重写<一个href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29"><$c$c>onSaveInstanceState()并保存活性的研究状态时,其名为:

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());
}

然后恢复状态的onCreate() onRestoreInstanceState()

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


如果这还不够,你可以覆盖 onRetainNonConfigurationInstance()键,返回将被传递到新的活动对象,当其重新创建任何自定义对象。关于如何使用它的更多细节可以在<一个被发现href="http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject">Handling运行时更改。但是,这个功能是pcated在安卓3.0+德$ P $(专为 FragmentActivity 其中,其最终)。因此,这不能与片段使用(这是很好的,他们有自己的机制来留住翻过配置更改的对象)。


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).

和最后一节 - 从来不使​​用安卓configChanges 。您必须使用它很好的理由,通常这些都是性能的原因。这是不是意味着被滥用,现在事情是这样的:刚prevent UI状态复位。如果这个属性被使用,那么是的,活动的UI将不会在配置变化重新设置,但活动状态还是会被破坏,后来重新创建时复位。

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.

本文档介绍了此选项pretty的还有:

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

这篇关于我的观点正在被复位方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆