Android Handling Runtime Changes
2015-12-03 00:01
465 查看
App运行时的状态改变
》Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When
such a change occurs, Android restarts the running
called, followed by
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.
》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:
Retain an object during a configuration change
Allow your activity to restart when a configuration changes, but carry a stateful object to the new instance of your activity.
Handle the configuration change yourself
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.
》You can add such fragments to your activity to preserve stateful objects.
To retain stateful objects in a fragment during a runtime configuration change:
Extend the
stateful objects.
Call
the fragment is created.
Add the fragment to your activity.
Use
the activity is restarted.
For example, define your fragment as follows:
》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
you must include the
That is, you must decalare
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).
》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
》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.
》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:
Retain an object during a configuration change
Allow your activity to restart when a configuration changes, but carry a stateful object to the new instance of your activity.
Handle the configuration change yourself
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.
》You can add such fragments to your activity to preserve stateful objects.
To retain stateful objects in a fragment during a runtime configuration change:
Extend the
Fragmentclass and declare references to your
stateful objects.
Call
setRetainInstance(boolean)when
the fragment is created.
Add the fragment to your activity.
Use
FragmentManagerto retrieve the fragment when
the activity is restarted.
For example, define your fragment as follows:
public class RetainedFragment extends Fragment { // data object we want to retain private MyDataObject data; // this method is only called once for this fragment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // retain this fragment setRetainInstance(true); } public void setData(MyDataObject data) { this.data = data; } public MyDataObject getData() { return data; } }
》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
minSdkVersionand
targetSdkVersionattributes),
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).
》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().
相关文章推荐
- Android Manifest之<service>元素中文注释
- android aidl接口初步了解
- Android java.lang.OutOfMemoryError: bitmap 解决方法
- Android Accessing Resources
- 初学安卓
- Android Studio安装与使用(二)
- 通过api查看android控件属性
- Android设置Settings:预读取设置的选项和更新设置结果【2】
- (转)Android SlidingTabLayout定制分割线和指示条颜色
- android 应用程序的组成部分
- openwebrtc 服务端和android客户端demo安装,openwebrtcandroid
- Android Providing Resources
- Android 上传文件,图片。以及服务器端接收相关
- android中的HandlerThread类的学习
- android Resources Overview
- Android设置Settings实现:PreferenceActivity【1】
- Fragment生命周期应用交互详解
- Android之安全退出应用程序的几种方式
- 使Android 自带SDK 完美支持HTML5 之 html5webview
- Android中基于protobuf的socket通信的实例