翻译Activity

Configuration Changes

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to bedestroyed, going through the normal activity lifecycle process of onPause(),onStop(), andonDestroy() as appropriate. If the activity had been in the foreground or visible to the user, onceonDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with theandroid:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted andonConfigurationChanged(Configuration) will not be called.

Starting Activities and Getting Results

The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call thestartActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent'sActivity.onActivityResult(), along with the integer identifier it originally supplied.

If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

译文:

配置的改变

如果设备的配置发生了改变(定义的资源。配置类),然后任何显示用户界面需要更新以匹配配置。因为Activity(活动)是与用户交互的主要机制,尤其包括支持操作系统配置的改变。

除非你指定,一个配置的改变将会导致你当前线程被销毁,经理的正常活动生命周期过程onPause(),onStop()和onDestroy()方法。如果Activity已经在前台或用户可见的,一旦执行onDestroy()方法将创建一个新的Activity实例,例如从onSaveInstanceState包生成的savedInstanceState的实例。

这样做是因为任何应用程序资源,包括布局文件,基于任何配置值都可以改变。因此处理配置高钙的唯一安全的方法是重新检索所有的资源,包括布局,视图和字符串。因为线程必须已经知道了怎么样保存状态并重新创建自己的状态,这是一个便捷的方法来启动一个activity来重启一个新的配置。

在某些特殊的情况下,你可能想要重启你的活动绕过一个锅多个类型的配置更改。这是完成了android:configChanges的属性清单。对于你解决任意配置类型的改变,你将会收到调用当前活动的onConfigurationChanged(Configuration)方法而不是重新启动。如果一个配置改变设计到你无法解决时,但是,activity还是要被重启并且onConfigurationChanged(配置)将不会被调用。

开启活动并得到结果

startActivity(Intent)方法用于启动一个新活动,这个活动将会被放在activity堆栈顶。这需要一个参数,一个意图,描述这个被执行的活动。

有时候你想在一个活动结束的时候得到一个结果。比如,你可能开启一个让用户选择一个人在一个人联系列表的活动;当他结束,返回用户选择的。要做到这点,你用startActivityForResult(Intent,int)版本,第二个整数参数识别。结果会回来通过调用onActivityResult(int,int,intent)方法。

当退出activity时,它可以调用setResult(int)返回数据给它的父类。他必须提供一个结果代码,可以作为标准结果的RESULT_CANCELED, RESULT_OK,或者任何自定义值RESULT_FIRST_USER。另外,它可以随意的返回包括任何额外的数据的意图。所有这些信息都会在父类的Activity.onActivityResult()方法中显示,随着它最初提供的整数标示符。

如果一个子活动因为任何原因失败(例如崩溃),父类的活动将受到一个结果关于RESULT_CANCELED的代码。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值