`

【翻译】(30)处理运行时改变

 
阅读更多

【翻译】(30)处理运行时改变

 

see

http://developer.android.com/guide/topics/resources/runtime-changes.html

 

原文见

http://developer.android.com/guide/topics/resources/runtime-changes.html

 

-------------------------------

 

Handling Runtime Changes

 

处理运行时改变

 

-------------------------------

 

In this document

 

本文目录

 

* Retaining an Object During a Configuration Change 在配置改变期间保持一个对象

* Handling the Configuration Change Yourself 自己处理配置改变

 

See also

 

另见

 

Providing Resources 提供资源

Accessing Resources 访问资源

Faster Screen Orientation Change 更快的屏幕方向改变

 

-------------------------------

 

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

 

一些设备配置可以在运行时改变(诸如屏幕方向,键盘可用性,以及语言)。当这样的改变发生时,Android重启正在运行的Activity(onDestroy()被调用,然后是onCreate())。重启行为被设计为有助于你的应用程序通过用匹配新的设备配置的可选资源自动地重新加载你的应用程序,适配新的配置。

 

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

 

为了合理地处理重启,重要的是你的活动恢复它前一个状态,贯穿普通的Activity生命周期,其中Android在它销毁你的活动之前调用onSaveInstanceState(),使你可以保存关于应用程序状态的数据。然后你可以在onCreate()或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. Your application should be able to restart at any time without loss of user data or state in order to handle events such as configuration changes or when the user receives an incoming phone call and then returns to your application much later after your application process may have been destroyed. To learn how you can restore your activity state, read about the Activity lifecycle.

 

为了测试你的应用程序用应用程序的完整状态重启它自己,你应该在执行你的程序中的不同任务时请求配置改变(诸如改变屏幕大小)。你的应用程序应该可以在任何时候重启而不丢失用户数据或状态,以便处理事件诸如配置改变或者在用户接收到一个呼入的电话呼叫的时候,然后过了很久,你的应用程序可能已经被销毁后,返回到你的应用程序。要想知道你如何可以恢复你的活动状态,请阅读关于活动的生命周期的章节。

 

However, you might encounter a situation in which restarting your application and restoring significant amounts of data can be costly and create a poor user experience. In such a situation, you have two other options:

 

然而,你可能遇到一个情况,重启你的应用程序并且恢复大量数据可能代价昂贵而且造成一个差的用户体验。在这种情况下,你有其它两个选择:

 

a. Retain an object during a configuration change

 

a. 在配置改变期间保持一个对象

 

Allow your activity to restart when a configuration changes, but carry a stateful Object to the new instance of your activity.

 

当一个配置改变时允许你的活动重启,但把一个带状态Object搬到你的活动的新实例。

 

b. Handle the configuration change yourself

 

b. 自己处理配置改变

 

Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the configurations do change, so that you can manually update your activity as necessary.

 

在某个配置改变期间阻止系统重启你的活动,但在配置作出改变时接收回调,致使你可以在需要时手动地更新你的活动。

 

-------------------------------

 

Retaining an Object During a Configuration Change

 

在配置改变期间维持一个Object

 

If restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform other intensive operations, then a full restart due to a configuration change might be a slow user experience. Also, it might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with the onSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a stateful Object when your activity is restarted due to a configuration change.

 

如果重启你的活动需要你恢复大量数据,重新建立一个网络连接,或执行其它密集操作,那么一个由配置改变导致的完全重启可能是一个缓慢的用户体验。同样,使用系统用onSaveInstanceState()为你保存的Bundle来完全地恢复你的活动状态,可能对于你来说是不可能的——它不是设计来携带大对象(诸如位图)而它里面的数据必须被序列化然后反序列化,它可能消耗大量内存并且导致配置改变变慢。在这种情况下,你可以通过在你的活动由配置改变导致被重启的时候维持一个带状态的Object,减轻重新初始化你的活动的负担。

 

To retain an object during a runtime configuration change:

 

要想在运行时配置改变期间维持一个对象:

 

1. Override the onRetainNonConfigurationInstance() method to return the object you would like to retain.

 

1. 覆盖onRetainNonConfigurationInstance()方法以返回你希望维持的对象。

 

2. When your activity is created again, call getLastNonConfigurationInstance() to recover your object.

 

2. 当你的活动被再次创建时,调用getLastNonConfigurationInstance()以恢复你的对象。

 

When the Android system shuts down your activity due to a configuration change, it calls onRetainNonConfigurationInstance() between the onStop() and onDestroy() callbacks. In your implementation of onRetainNonConfigurationInstance(), you can return any Object that you need in order to efficiently restore your state after the configuration change.

 

当Android系统因为一个配置改变而关闭你的活动时,它在onStop()和onDestroy()回调之间调用onRetainNonConfigurationInstance()。在你的onRetainNonConfigurationInstance()实现里,你可以返回你需要的任意对象以便在配置改变后有效地恢复你的状态。

 

A scenario in which this can be valuable is if your application loads a lot of data from the web. If the user changes the orientation of the device and the activity restarts, your application must re-fetch the data, which could be slow. What you can do instead is implement onRetainNonConfigurationInstance() to return an object carrying your data and then retrieve the data when your activity starts again with getLastNonConfigurationInstance(). For example:

 

一个它可能有用的场景是假如你的应用程序从网页上加载许多数据。如果用户改变设备的方向而活动重启,那么你的应用程序必须重新获取数据,这可能很慢。相反,你可以做的是实现onRetainNonConfigurationInstance()以返回一个携带你的数据的对象,然后在活动再次启动的时候用getLastNonConfigurationInstance()取出数据。例如:

 

-------------------------------

 

@Override

public Object onRetainNonConfigurationInstance() {

    final MyDataObject data = collectMyLoadedData();

    return data;

}

 

-------------------------------

 

-------------------------------

 

Caution: While you can return any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)

 

警告:虽然你可以返回任意对象,但你应该从不传递一个关联到Activity的对象,诸如一个Drawable,一个Adapter,一个View或其它任意与Context关联的对象。如果你这样做,它将泄漏原始活动实例的所有视图和资源。(泄漏资源意味着你的应用程序维持对它们的控制而它们不能被垃圾回收,这样大量内存可能被丢失。)

 

-------------------------------

 

Then retrieve the data when your activity starts again:

 

然后在你的活动再次启动时取出数据:

 

-------------------------------

 

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

 

    final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();

    if (data == null) {

        data = loadMyData();

    }

    ...

}

 

-------------------------------

 

In this case, getLastNonConfigurationInstance() returns the data saved by onRetainNonConfigurationInstance(). If data is null (which happens when the activity starts due to any reason other than a configuration change) then this code loads the data object from the original source.

 

在这种情况下,getLastNonConfigurationInstance()返回被onRetainNonConfigurationInstance()保存的数据。如果数据是null(它发生在活动由其它非配置改变的原因导致启动的时候),那么这段代码从原来的源中加载数据。

 

-------------------------------

 

Handling the Configuration Change Yourself

 

自己处理配置改变

 

If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.

 

如果你的应用程序不需要在一个特定配置改变期间更新资源,而且你有一个性能限制,需要你避免活动重启,那么你可以声明你的应用自己处理配置改变,它阻止系统重启你的活动。

 

-------------------------------

 

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 when you must avoid restarts due to a configuration change and is not recommended for most applications.

 

注意:自己处理配置改变可能导致使用可选资源变得更加困难,因为系统不会自动为你应用它们。这项技术应该被认为是在你必须避免因为配置改变而重启的时候的最后手段,而且对于大多数应用程序是不推荐的。

 

-------------------------------

 

To declare that your activity handles a configuration change, edit the appropriate <activity> element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.

 

为了声明你的活动处理配置改变,编辑你的清单文件中合适的<activity>元素以包含android:configChanges属性,它带有代表你希望处理的配置的值。可能的值列举在android:configChanges属性的文档中(最常用的值是“orientation”,用于阻止屏幕方向改变时的重启,以及“keyboardHidden”,用于阻止键盘可用性改变时的重启)。你可以通过用一个管道“|”字符分隔它们,在这个属性中声明多个配置值。

 

For example, the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:

 

例如,以下清单代码声明一个活动处理屏幕方向改变和键盘可用性改变:

 

-------------------------------

 

<activity android:name=".MyActivity"

          android:configChanges="orientation|keyboardHidden"

          android:label="@string/app_name">

 

-------------------------------

 

Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.

 

现在,当其中一种配置改变时,MyActivity不重启。取而代之,MyActivity接收一个对onConfigurationChanged()的调用。这个方法传递进一个Configuration对象,它指定新的设备配置。通过读取Configuration的域,你可以确定新的配置并且通过更新你的界面中的资源来作出合适的改变。在这个方法被调用的时候,你的活动的Resources对象基于新的配置来更新以返回资源,使你可以简单地重置你的用户界面的元素,而不是系统重启你的活动。

 

-------------------------------

 

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

 

警告:从Android 3.2(API级别13)开始,“屏幕大小”也在设备在竖屏和宽屏之间切换时改变。这样,如果你希望在为API级别13或更高的版本(正如minSdkVersion和targetSdkVersion属性所声明的那样)开发时阻止由方向改变导致的运行时重启,那么你必须包含“screenSize”添加到“orientation”值后面。就是说,你必须声明android:configChanges="orientation|screenSize"。然而,如果你的应用程序的目标为API级别12或更低,那么你的活动总是自己处理这个配置改变(这个配置改变不重启你的活动,即使运行在Android 3.2或更高的设备上)。

 

-------------------------------

 

For example, the following onConfigurationChanged() implementation checks the current device orientation:

 

例如,以下onConfigurationChanged()实现检查当前设备方向:

 

-------------------------------

 

@Override

public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);

 

    // Checks the orientation of the screen

    // 检查屏幕方向

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();

    }

}

 

-------------------------------

 

The Configuration object represents all of the current configurations, not just the ones that have changed. Most of the time, you won't care exactly how the configuration has changed and can simply re-assign all your resources that provide alternatives to the configuration that you're handling. For example, because the Resources object is now updated, you can reset any ImageViews with setImageResource() and the appropriate resource for the new configuration is used (as described in Providing Resources).

 

Configuration对象表示当前的所有配置,不只是已经改变的那些。大多数时候,你将不会确切地关心配置是如何改变的,并且可以简单地重新赋予所有你提供可选项给你正在处理的配置的资源。例如,因为Resources对象现在被更新,你可以用setImageResource()重置任意ImageView,而且合适资源会被用于新的配置(正如提供资源中所描述的那样)。

 

Notice that the values from the Configuration fields are integers that are matched to specific constants from the Configuration class. For documentation about which constants to use with each field, refer to the appropriate field in the Configuration reference.

 

注意来自Configuration的域的值是整型,它匹配来自Configuration类的特定常量。想获取关于每个域使用哪个常量的文档,请参考Configuration参考文档中合适的域。

 

-------------------------------

 

Remember: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged().

 

记住:当你声明你的活动以处理配置改变时,你有责任重置你为其提供可选项的任意元素。如果你声明你的活动以处理方向改变,并且有图片应该在宽屏和竖屏之间改变,那么你必须在onConfigurationChanged()期间重新赋予每个资源到每个元素。

 

-------------------------------

 

If you don't need to update your application based on these configuration changes, you can instead not implement onConfigurationChanged(). In which case, all of the resources used before the configuration change are still used and you've only avoided the restart of your activity. However, your application should always be able to shutdown and restart with its previous state intact, so you should not consider this technique an escape from retaining your state during normal activity lifecycle. Not only because there are other configuration changes that you cannot prevent from restarting your application, but also because you should handle events such as when the user leaves your application and it gets destroyed before the user returns to it.

 

如果你不需要基于这些配置改变更新你的应用程序,那么取而代之,你可以不实现onConfigurationChanged()。在这种情况下,在配置改变前使用的所有资源仍然被使用,而你不仅仅避免了重启你的活动。然而,你的应用程序应该总是能够关闭和完整地用它的前一个状态重启,所以你不应该认为这项技术是逃避维护你在正常活动生命周期期间的状态的方法。不仅仅因为存在你无法阻止重启你的应用程序的其它配置改变,还因为你应该处理一些事件,诸如当用户离开你的应用程序时,在用户返回到它之前它被销毁。

 

For more about which configuration changes you can handle in your activity, see the android:configChanges documentation and the Configuration class.

 

想获取关于你可以在活动中处理哪些配置改变的更多信息,请参见android:configChanges文档以及Configuration类。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 04 Jan 2012 0:53

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/

 

分享到:
评论

相关推荐

    Trados 自动翻译使用详细说明

    首先,需确保MultiTerm正常运行,以便在翻译过程中进行术语识别。步骤如下: - 启动MultiTerm:通过开始菜单中的“TRADOSfinetranslationtools”启动MultiTermEnglish。 - 加载数据库:选择Training目录下的Trados....

    一个ComfyUI的多语言翻译插件,这个插件实现了居民菜单

    这个ComfyUI插件很可能就是基于JavaScript构建的,利用了其强大的DOM操作能力和异步通信能力,实现在运行时加载并应用相应的翻译资源。 居民菜单(Resident Menu)通常指的是软件或应用中与用户角色相关的功能集合...

    ARM指令集翻译器

    "ARM指令集翻译器"是指一个工具,它的功能是将特定格式的代码(可能是二进制或汇编)转换成ARM架构下的机器码,以便在ARM处理器上运行。在这个场景中,描述提到的"bea文件夹下所有复制解压到/system/lib就能使用...

    甲骨文计算机辅助翻译技术研究.pdf

    2. **软件本地化**:甲骨文的工具能有效处理软件界面、帮助文档和用户手册的翻译,确保软件在不同语言环境下正常运行。 3. **网站内容更新**:网站和电子商务平台的多语言版本维护,可以通过甲骨文的CAT工具实现...

    thinking in java 4th 样章的翻译

    异常处理是处理程序运行时错误的关键机制,帮助我们编写健壮的代码。集合框架如ArrayList、LinkedList、HashMap等是存储和管理对象的常用工具。泛型的引入提升了代码类型安全,减少了强制类型转换的麻烦。IO流处理...

    PHP多语言翻译 和php的gettext函数类似

    它通过查找与程序运行时环境相匹配的语言环境来选择正确的翻译字符串。这个过程涉及到两个主要的文件格式:`.po`(Portable Object)和`.mo`(Machine Object)。 `.po`文件是文本编辑器(如Poedit)创建的源文件,...

    外文翻译城市污水常温处理中的新型改良EGSB膨胀颗粒污泥床反应器的发展毕业论文.doc

    本文主要探讨了一种新型改良的膨胀颗粒污泥床(EGSB)反应器在外文翻译城市污水常温处理中的发展。厌氧处理技术因其低能耗和高沼气产量成为可持续污水处理的重要方法,其中EGSB反应器因其在COD去除效率和抗负荷能力...

    Python-500lineorless中文翻译计划

    5. **错误与异常处理**:通过try-except语句来捕获和处理运行时错误,确保程序的健壮性。 6. **模块与包**:学习如何组织代码,导入和使用外部模块,以及创建自定义模块和包。 7. **文件操作**:包括读写文件,...

    侯捷翻译的经典IT中文书籍大全

    理解对象模型对于优化代码和解决运行时问题至关重要。 4. **重构**:《重构:改善既有代码的设计》阐述了重构的重要性,即在不改变软件外部行为的前提下改进其内部结构,以提高代码可读性和维护性。这本书提供了...

    Unity3D中英文翻译API

    8. **资源管理**:AssetBundle系统允许在运行时动态加载和卸载资源,优化游戏性能,减少初始加载时间。 9. **UI系统**:Unity的UI系统(UGUI)基于Canvas和各种UI元素,如Text、Image、Button等,可以轻松创建用户...

    nodejs自动批量翻译中文到其它语言.pdf

    - 谷歌翻译接口通过改变页面上的源语言输入框的值来提交翻译请求,然后从目标语言的输出框中获取翻译结果。 这个方法虽然有效,但存在一些潜在问题和优化空间: 1. 性能:由于是逐个文件逐行翻译,效率可能较低,...

    中英文切换,自定义翻译词典

    在程序运行时,根据用户的选择动态加载相应的资源文件,从而改变界面上的文字内容。实现这一功能的关键是正确管理资源文件,并在用户切换语言时更新界面控件的Text属性。 其次,**自定义翻译词典**是允许用户编辑和...

    Birt的开发环境和运行环境汉化

    - **步骤三:**对于动态数据,可能需要在脚本中处理,比如通过Java或JavaScript设置locale来改变语言环境,以便正确显示和格式化日期、数字等。 - **步骤四:**在Web应用程序中,确保服务器支持多语言环境,可以...

    编程术语英语翻译

    12. **异常(Exception)**:程序运行时出现的错误,通过 `try-catch` 语句捕获并处理。 13. **算法(Algorithm)**:解决问题或完成任务的明确步骤和指令集,如排序算法(冒泡排序、快速排序等)。 14. **编译器...

    java类外文翻译

    "java类外文翻译 Business Applications of Java" 本资源摘要信息主要介绍Java技术在商业应用中的应用,涵盖了Java技术的历史、影响、组件、概念和应用领域。下面是相关知识点: 1. Java技术的历史:Java技术的...

    VScode说明文档翻译1

    用户可以方便地报告问题,参与翻译项目,甚至开发自己的扩展,共同推动VSCode的发展。 【定制化】 VSCode的可定制性非常强,用户可以自定义工作区设置,针对不同的项目保存不同的配置。此外,用户可以创建自定义...

    电气工程及其自动化专业英语翻译

    3. 数字信号处理(Digital Signal Processing, DSP):在电气自动化中,用于分析、滤波和控制信号的技术。 4. 集成电路(Integrated Circuit, IC):将多个电子元件集成在一个小芯片上的技术。 5. 可编程逻辑控制器...

    安卓字体使用多语言相关-动态改变字体的状态.zip

    1. **动态改变字体**:在安卓应用中,动态改变字体意味着在运行时根据用户需求或应用逻辑调整字体样式,包括字体类型、大小、颜色等。这通常通过修改TextView或其他文本视图的属性来实现,如` Typeface....

    易语言源代码_易在线翻译机.zip

    变量和常量则用于存储数据,其中变量的值可以在程序运行过程中改变,而常量的值一旦设定就不可更改。表达式用于计算和比较,而语句则构成了程序的基本执行单元,比如赋值语句、条件判断语句和循环语句等。 "易在线...

Global site tag (gtag.js) - Google Analytics