英文:
saveInstanceState + restoreInstanceState VS configChanges
问题
我遇到了两种方法,可以防止活动在屏幕旋转时重新绘制。其中之一是saveInstanceState + restoreInstanceState组合,我仍然无法成功实现。另一种方法是在清单文件的活动标签中声明configChanges属性为orientation。我很好奇这两种方法之间的区别。
英文:
I came across two ways to prevent the activity from redrawing whenever the screen is rotated.
One is saveInstanceState + restoreInstanceState combo, which I still haven't been able to successfully implement.
The other one was to declare configChanges attribute in the activity tag of the manifest file to orientation
I'm curious as to what is the difference between the two.
答案1
得分: 0
这两种解决方案都涉及到在您的活动生命周期内保持数据的持久性。请阅读这里以了解有关生命周期的更多信息。
基本区别在于,当您的应用程序停止运行时,使用save/restoreInstance组合(有许多不同的方法可以实现)可以保存您的应用程序的状态,当您的活动在之前被销毁后重新创建时,您可以从系统传递给您的活动的Bundle中恢复保存的实例状态。
configChanges
列出了活动将自行处理的配置更改。在运行时发生配置更改时,默认情况下会关闭并重新启动活动,但声明具有此属性的配置将防止活动重新启动。来源
一般而言,我建议不要使用configChanges属性锁定应用程序的方向,以防止您的活动停止运行。只需通过生命周期保存其状态并进行恢复。
英文:
Both of the solutions have to do with data persistence during the lifecycle of your activity. Please read here more regarding LifeCycle
The basic difference is that when your app dies, with the save/restoreInstance combo (there are many different ways to do this) is that you can save the state of your app and when your activity is recreated after it was previously destroyed, you can recover your saved instance state from the Bundle that the system passes to your activity.
configChanges
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. source
Generally speaking i would suggest to NOT lock orientation in your app with the configChanges attr to prevent your activity from dying. Just save its state and restore through LifeCycle
答案2
得分: 0
The difference between saveInstanceState + restoreInstanceState combo and configChanges is that configChanges属性在清单中会防止在指定的更改发生时销毁活动。在Android中,当您旋转设备或屏幕尺寸发生变化时,活动将被销毁并重新创建。
saveInstanceState + restoreInstanceState组合不会防止活动被销毁和重新创建。实际上,它们只会帮助您将数据从被销毁的活动传递到将要重新创建的活动中。
在saveInstanceState中,您可以将要保存的所有数据保存到bundle对象中。然后,在restoreInstanceState中,您可以获取这些数据并在重新创建的活动中使用它们。
例如,如果我在清单文件中的活动标签中使用此属性:
android:configChanges="orientation|screenSize"
那么,当屏幕旋转时,这个活动将不会被销毁,活动的所有数据都会保持不变,因为它不会被销毁。
这是关于saveInstanceState + restoreInstanceState组合的信息。
英文:
> I'm curious as to what is the difference between the two.
The difference between saveInstanceState + restoreInstanceState combo and configChanges is that configcahnges attribute in the manifest will prevent the activity from being destroyed when the specified change(s) occurred. In Android when you rotate the device or when the screen size change, the activity will be destroyed and recreated.
saveInstanceState + restoreInstanceState combo will not prevent the activity from being destroyed and recreated. In fact, they will just help you pass the data from the destroyed activity to the one which will be recreated.
Inside saveInstanceState you save all the data you want into a bundle object. Then, inside restoreInstanceState you get these data and use them in the recreated activity.
For example, if I use this attribute inside an activity tag in the Manifest file:
android:configChanges="orientation|screenSize"
Now, this activity won't be destroyed when the screen rotates and all the data of the activity will stay as is because it will not be destroyed.
> saveInstanceState + restoreInstanceState combo, which I still haven't been able to successfully implement.
Here is how saveInstanceState + restoreInstanceState combo work:
Let's say I have a global variable called userScore
holding the user scores in a game. Now, I did not use the attribute android:configChanges="orientation|screenSize"
in my activity tag, so it will be destroyed if the user rotates the screen and the scores will be lost. To prevent them from being lost I will use saveInstanceState + restoreInstanceState like so:
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putInt("Score", userScore); // saving the userScore value
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
userScore = savedInstanceState.getInt("Score"); // restoring the userScore value
super.onRestoreInstanceState(savedInstanceState);
}
答案3
得分: 0
在某些情况下,您正在处理所有相关的配置更改,而无需重新启动活动。而在另一种情况下,您只需要处理所需的用户状态变量,即保存和恢复它们。
当运行时发生配置更改时,默认情况下会关闭并重新启动活动,但是使用android:configChanges属性声明配置将阻止活动被重新启动。相反,活动保持运行,并调用其 onConfigurationChanged() 方法,自行处理配置更改是一项复杂的任务,应该避免。
由于处理配置更改的隐藏复杂性,不建议自行处理配置更改。
然而,如果您无法使用首选选项(onSaveInstanceState()、ViewModel 和持久存储)保留UI状态,您可以在某些配置更改期间阻止系统重新启动您的活动。您的应用将在配置更改时接收回调,以便您可以根据需要手动更新活动。
英文:
In one scenarios you are handling all the
Related configuration changes without activity getting restarted.. while in second you are just taking care of your desired user state variables ie saving and restoring them.
When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with android:configChanges attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called, handling configuration changes your self is a complex task and should be avoided..
It is not recommended to handle configuration changes yourself due to the hidden complexity of handling the configuration changes.
However, if you are unable to preserve your UI state using the preferred options (onSaveInstanceState(), ViewModels, and persistent storage) you can instead prevent the system from restarting your activity during certain configuration changes. Your app will receive a callback when the configurations do change so that you can manually update your activity as necessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论