处理显示尺寸变化
Handling Display Size change
我正在将我的应用目标 SDK 版本迁移到 android oreo。
其中一项可能会影响应用程序的更改是,如果设备的显示尺寸发生变化,则针对 Android 牛轧糖及更高版本的应用程序将收到通知,就好像屏幕方向发生了变化一样设备。以下是来源。
When the device density changes, the system notifies running apps in
the following ways:
If an app targets API level 23 or lower, the system automatically
kills all its background processes. This means that if a user switches
away from such an app to open the Settings screen and changes the
Display size setting, the system kills the app in the same manner that
it would in a low-memory situation. If the app has any foreground
processes, the system notifies those processes of the configuration
change as described in Handling Runtime Changes, just as if the
device's orientation had changed.
If an app targets Android 7.0, all
of its processes (foreground and background) are notified of the
configuration change as described in Handling Runtime Changes.
现在我的应用程序有一个长 运行 进程,它在 Android Activity 中的异步 TSak 中运行。这意味着应用程序在 Activity 代码中有一个 AsyncTask。
我在异步任务开始时创建了一个对话框,并在异步任务完成后隐藏它。
现在假设用户开始任务然后转到设置并更改显示大小然后 return 回到我的应用程序然后应用程序的对话框消失了异步任务仍然执行直到end 这意味着用户可能认为应用程序已经完成了任务,但应用程序实际上正在执行任务。但是我的 Activity 也像重新启动一样响应,除了异步任务是 运行。
我的应用程序的方向锁定为纵向。
遇到这种情况应该怎么处理?任何帮助表示赞赏。谢谢!
编辑:这是场景
1) 应用启动
2) 主屏幕
3) 用户按下按钮
4) 主屏幕内的 AsyncTask 已启动
5) 显示任务进度的对话框
6) 用户最小化应用程序并转到设置
7) 更改显示 Size/Density 或更改设备的字体大小
8) 我的应用程序被 OS 调用,就像设备旋转发生变化一样
9) 然后用户 return 返回应用程序
10) 但是显示的对话框不再显示
11) 但异步任务 运行 在后台并且仍在执行其任务
12) 任务确实完成了,但用户认为任务还没有完成。
在您的 on_resume() 方法中验证屏幕大小和字体大小没有改变。如果他们有,请适当地调整、删除和重新创建您的对话框。这类似于在暂停时处理方向更改(添加字体大小作为条件)。
编辑:
将 android:configChanges="fontScale|density"
添加到您的清单文件。
static int fontScale = 1;
static int densityDpi = 0;
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.fontScale != fontScale) {
// destroy and recreate dialog adjusting for font size.
fontScale = newConfig.fontScale;
}
if (newConfig.densityDpi != densityDpi) {
// destroy and recreate dialog adjusting for new screen size.
densityDpi = newConfig.densityDpi;
}
}
对异步任务使用通知可能会更好,因为它们不受配置更改的影响,并且不会在内存不足的情况下终止。
当您旋转 phone 时,您的 activity 实际上会被销毁并重新创建。这包括您的对话。有两种修复方法:
1) 关闭此 "helpful" 功能。如果您没有不同的横向和纵向布局,我建议这样做。将 android:configChange="orientation|resize" 添加到清单中的 activity。
2) 实施 onSaveInstanceState/onRestoreInstanceState 并有一个变量说明您是否需要重新创建和重新启动对话框。
建议:
尝试使用 WorkManager 来安排任务以及来自 android 架构组件的 ViewModel,这有助于即使在配置更改后也能保持状态
我正在将我的应用目标 SDK 版本迁移到 android oreo。
其中一项可能会影响应用程序的更改是,如果设备的显示尺寸发生变化,则针对 Android 牛轧糖及更高版本的应用程序将收到通知,就好像屏幕方向发生了变化一样设备。以下是来源。
When the device density changes, the system notifies running apps in the following ways:
If an app targets API level 23 or lower, the system automatically kills all its background processes. This means that if a user switches away from such an app to open the Settings screen and changes the Display size setting, the system kills the app in the same manner that it would in a low-memory situation. If the app has any foreground processes, the system notifies those processes of the configuration change as described in Handling Runtime Changes, just as if the device's orientation had changed. If an app targets Android 7.0, all of its processes (foreground and background) are notified of the configuration change as described in Handling Runtime Changes.
现在我的应用程序有一个长 运行 进程,它在 Android Activity 中的异步 TSak 中运行。这意味着应用程序在 Activity 代码中有一个 AsyncTask。
我在异步任务开始时创建了一个对话框,并在异步任务完成后隐藏它。
现在假设用户开始任务然后转到设置并更改显示大小然后 return 回到我的应用程序然后应用程序的对话框消失了异步任务仍然执行直到end 这意味着用户可能认为应用程序已经完成了任务,但应用程序实际上正在执行任务。但是我的 Activity 也像重新启动一样响应,除了异步任务是 运行。
我的应用程序的方向锁定为纵向。
遇到这种情况应该怎么处理?任何帮助表示赞赏。谢谢!
编辑:这是场景
1) 应用启动
2) 主屏幕
3) 用户按下按钮
4) 主屏幕内的 AsyncTask 已启动
5) 显示任务进度的对话框
6) 用户最小化应用程序并转到设置
7) 更改显示 Size/Density 或更改设备的字体大小
8) 我的应用程序被 OS 调用,就像设备旋转发生变化一样
9) 然后用户 return 返回应用程序
10) 但是显示的对话框不再显示
11) 但异步任务 运行 在后台并且仍在执行其任务
12) 任务确实完成了,但用户认为任务还没有完成。
在您的 on_resume() 方法中验证屏幕大小和字体大小没有改变。如果他们有,请适当地调整、删除和重新创建您的对话框。这类似于在暂停时处理方向更改(添加字体大小作为条件)。
编辑:
将 android:configChanges="fontScale|density"
添加到您的清单文件。
static int fontScale = 1;
static int densityDpi = 0;
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.fontScale != fontScale) {
// destroy and recreate dialog adjusting for font size.
fontScale = newConfig.fontScale;
}
if (newConfig.densityDpi != densityDpi) {
// destroy and recreate dialog adjusting for new screen size.
densityDpi = newConfig.densityDpi;
}
}
对异步任务使用通知可能会更好,因为它们不受配置更改的影响,并且不会在内存不足的情况下终止。
当您旋转 phone 时,您的 activity 实际上会被销毁并重新创建。这包括您的对话。有两种修复方法:
1) 关闭此 "helpful" 功能。如果您没有不同的横向和纵向布局,我建议这样做。将 android:configChange="orientation|resize" 添加到清单中的 activity。
2) 实施 onSaveInstanceState/onRestoreInstanceState 并有一个变量说明您是否需要重新创建和重新启动对话框。
建议: 尝试使用 WorkManager 来安排任务以及来自 android 架构组件的 ViewModel,这有助于即使在配置更改后也能保持状态