显示相关
屏幕朝向
https://developer.android.com/reference/android/content/res/Configuration.html#orientation
具体区别如下:
activity.getResources().getConfiguration().orientation获取的是当前设备的实际屏幕方向值,可以动态地根据设备的旋转或用户的操作进行改变。例如,当用户将设备从纵向旋转到横向时,获取到的屏幕方向值也会相应地改变。
Manifest中配置的orientation是用于指定活动的默认方向,即在没有其他因素影响时,活动应该显示的方向。它可以有以下几个值:
portrait:纵向(竖屏)方向。
landscape:横向(横屏)方向。
sensor:根据设备的旋转自动调整方向。
user:根据用户的偏好自动调整方向。这个配置可以在Manifest文件中的标签中进行设置。但是,它只是一个默认值,实际的屏幕方向还会受到其他因素的影响,如设备旋转、用户操作等。
activity.getResources().getConfiguration().orientation获取的是当前设备的屏幕方向值,而Manifest中配置的orientation是用于指定活动的默认方向。
总结来说,activity.getResources().getConfiguration().orientation获取的是当前设备的实际屏幕方向值,而Manifest中配置的orientation是活动的默认方向,但实际方向可能会根据设备和用户的操作而改变。
Android屏幕朝向
ORIENTATION_UNDEFINED:未指定
ORIENTATION_PORTRAIT:竖屏
ORIENTATION_LANDSCAPE:横屏
ORIENTATION_SQUARE:正方形
/** Constant for {@link #orientation}: a value indicating that no value has been set. */
public static final int ORIENTATION_UNDEFINED = 0;
/** Constant for {@link #orientation}, value corresponding to the
* <a href="{@docRoot}guide/topics/resources/providing-resources.html#OrientationQualifier">port</a>
* resource qualifier. */
public static final int ORIENTATION_PORTRAIT = 1;
/** Constant for {@link #orientation}, value corresponding to the
* <a href="{@docRoot}guide/topics/resources/providing-resources.html#OrientationQualifier">land</a>
* resource qualifier. */
public static final int ORIENTATION_LANDSCAPE = 2;
/** @deprecated Not currently supported or used. */
@Deprecated public static final int ORIENTATION_SQUARE = 3;
官方
android:screenOrientation
Activity 在设备上的显示方向。如果 activity 是在多窗口模式下运行,则系统会忽略该属性。在 Android 12(API 级别 31)及更高版本中,设备制造商可以将个别设备屏幕(例如,平板电脑大小的可折叠设备屏幕)配置为忽略屏幕方向指定,并强制应用在竖屏模式下垂直展示而在横屏模式下以信箱模式展示,这样便可保持应用所需的固定宽高比,但又能调整应用的屏幕方向以便用户更好地使用。
landscape(常用) 屏幕方向为横向(显示的宽度大于高度)。
portrait(常用) 屏幕方向为纵向(显示的高度大于宽度)。
sensor(常用) 屏幕方向由设备方向传感器决定。显示方向取决于用户如何手持设备,它会在用户旋转设备时发生变化。但在默认情况下,一些设备不会旋转为所有四种可能的方向。如要支持所有这四种方向,请使用 "fullSensor"。即使用户锁定基于传感器的旋转,系统仍可使用传感器。
fullSensor(常用) 屏幕方向由使用 4 种方向中任一方向的设备方向传感器决定。这与 "sensor" 类似,不同之处在于无论设备在正常情况下使用哪种方向,该值均支持所有 4 种可能的屏幕方向(例如,一些设备正常情况下不使用反向纵向或反向横向,但其支持这些方向)。API9 新增
unspecified 默认值。由系统选择方向。在不同设备上,系统使用的政策以及基于政策在特定上下文中所做的选择可能会有所差异。
behind 与 Activity 栈中紧接其后的 Activity 的方向相同。
reverseLandscape 屏幕方向是与正常横向方向相反的横向。API9 新增
reversePortrait 屏幕方向是与正常纵向方向相反的纵向。API9 新增
sensorLandscape 屏幕方向为横向,但可根据设备传感器调整为正常或反向的横向。即使用户锁定基于传感器的旋转,系统仍可使用传感器。API9 新增
sensorPortrait 屏幕方向为纵向,但可根据设备传感器调整为正常或反向的纵向。即使用户锁定基于传感器的旋转,系统仍可使用传感器。API9 新增
userLandscape 屏幕方向为横向,但可根据设备传感器和用户首选项调整为正常或反向的横向。API18 新增
userPortrait 屏幕方向为纵向,但可根据设备传感器和用户首选项调整为正常或反向的纵向。API18 新增
nosensor 确定屏幕方向时不考虑物理方向传感器。系统会忽略传感器,因此显示内容不会随用户手持设备的方向而旋转。
user 用户当前的首选方向。
fullUser 如果用户锁定基于传感器的旋转,则其行为与 user 相同,否则,其行为与 fullSensor 相同,并且支持所有 4 种可能的屏幕方向。API18 新增
locked 将屏幕方向锁定为其当前的任意旋转方向。API18 新增
注意:如果您声明其中一个横向或纵向值,则系统会将其视为对 activity 运行方向的硬性要求。 因此,您声明的值支持通过 Google Play 等服务进行过滤,这样只有支持 activity 规定方向的设备才能使用您的应用。例如,如果您声明 “landscape”、“reverseLandscape” 或 “sensorLandscape”,则您的应用只能供支持横向方向的设备使用。
此外,您还应通过 元素(例如 )明确声明,您的应用要求采用纵向还是横向方向。这是 Google Play(以及其他支持过滤的服务)提供的一种过滤行为,平台本身并不能控制是否可在仅支持某种方向的设备上安装您的应用。
android:configChanges
列出 activity 将自行处理的配置变更。在运行时发生配置变更时,默认情况下会关闭 activity 并将其重启,但使用该属性声明配置将阻止 activity 重启。相反,activity 会保持运行状态,并且系统会调用其 onConfigurationChanged() 方法。
注意:请仅在特殊情况下使用此属性,以提高应用性能和响应速度。如需了解详情,请参阅处理配置更改。
下列字符串是该属性的有效值。若有多个值,则使用 | 进行分隔,例如 “locale|navigation|orientation”。
“density” 显示密度的更改,例如当用户指定不同的显示比例或其他屏幕当前处于活跃状态时。 在 API 级别 24 中引入。
“fontScale” 字体缩放比例的更改,例如当用户选择新的全局字体大小时。
“keyboard” 键盘类型的更改,例如当用户插入外接键盘时。
“keyboardHidden” 键盘无障碍功能的更改,例如当用户显示硬件键盘时。
“layoutDirection” 布局方向的更改,例如从由左到右 (LTR) 改为由右到左 (RTL)。 在 API 级别 17 中引入。
“locale” 语言区域的更改,例如当用户选择显示文本所用的新语言时。
“mcc” 当系统检测到用于更新 MCC 的 SIM卡时,IMSI 移动设备国家/地区代码 (MCC) 发生的更改。
“mnc” 当系统检测到用于更新 MNC 的 SIM 卡时,IMSI移动网络代码 (MNC) 发生的更改。
“navigation” 导航类型(轨迹球或方向键)的 TA 更改。通常不会出现这种情况。
“orientation” 屏幕方向的更改,例如用户旋转设备时。
注意:如果应用面向 Android 3.2(API 级别 13)或更高版本的系统,则还应声明 “screenLayout” 和 “screenSize” 配置,因为当设备在纵向模式与横向模式之间切换时,屏幕布局和屏幕大小可能会发生变化。
“screenLayout” 屏幕布局的更改,例如在其他屏幕变为活动状态时。
“screenSize” 当前可用屏幕尺寸的更改。该值表示目前可用尺寸相对于当前宽高比的变更,当用户在横向模式与纵向模式之间切换时,它便会发生变更。在 API 级别 13 中引入。
“smallestScreenSize” 实体屏幕尺寸的更改。该值表示与方向无关的尺寸变更,因此它只有在实际物理屏幕尺寸发生变更(如切换到外部显示器)时才会变化。对此配置所作变更对应 smallestWidth 配置的更改。在 API 级别 13 中引入。
“touchscreen” 触摸屏的更改。通常不会出现这种情况。
“uiMode” 界面模式的更改,例如当用户将设备放到桌面或车载基座上时,或者夜间模式发生变化时。如需了解有关不同界面模式的更多信息,请参阅UiModeManager。 在 API 级别 8 中引入。
所有这些配置变更都可能影响应用所看到的资源值。因此,调用 onConfigurationChanged() 时,通常必须再次检索所有资源(包括视图布局和可绘制对象),才能正确处理更改。
注意:如要处理所有多窗口模式相关的配置变更,请使用 “screenLayout” 和 “smallestScreenSize”。Android 7.0(API 级别 24)或更高版本的系统支持多窗口模式。
方法 onConfigurationChanged
Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected android:configChanges you would like to handle with the R.attr.configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
方法 onSaveInstanceState
**Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).
The default implementation takes care of most of the UI per-instance state for you by calling View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.
If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P(28). For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().**
方法 onRestoreInstanceState
This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).
This method is called between onStart() and onPostCreate(Bundle). This method is called only when recreating an activity; the method isn’t invoked if onStart() is called for any other reason
Configuration
参考连接
横竖屏切换 屏幕适配 状态保存与恢复