Android: Efficient Screen Rotation Handling

泄露秘密 提交于 2019-11-30 19:48:45

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()).

  • To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreInstanceState().

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:

  1. 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.

  2. 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.

So, As per your data operation and coast of user experience with application you can choose what is best for you..

EDIT: As per your need for just change the layout I think you have to use onConfigurationChanged() and do the changes in it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!