安卓:高效的屏幕旋转处理 [英] Android: Efficient Screen Rotation Handling

查看:153
本文介绍了安卓:高效的屏幕旋转处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,当屏幕方向改变的的onCreate 方法被调用(横向和纵向的),如果处理不当,会重新创建整个活动时的结果过于简单地改变布局并保留所有的相同的信息。

In Android, when the screen orientation changes (between landscape and portrait) the onCreate method is called which ,if not handled properly, will recreate the entire activity when the result is too simply change the layout and retain all of the same information.

据我所知,有一些方法来解决这个问题,但我感兴趣的是最有效的方式。

I know of a few ways to solve this, but I'm interested in the most efficient way.

1)告诉清单,我会处理的方向变化通过覆盖 onConfigurationChanged()法和留空白。

1) Telling the manifest that I will handle the orientation change by overriding the onConfigurationChanged() method and leaving it empty.

2)覆盖的onSaveInstanceState()并保存在这里的数据以的onCreate被重新填充()

2) override onSaveInstanceState() and save the data here to be repopulated in onCreate()

3)覆盖的onPause()这将创建数据包并将其发送到的onSaveInstanceState()

3) Override onPause() which will create a bundle of the data and send it to onSaveInstanceState()

显然,这些是所有选项,但哪一个是一个更好的做法(小,中,大量的数据)

Obviously, these are all options, but which one is a better practice (small, medium, large amounts of data)

感谢您

推荐答案

有些设备配置可以在运行时改变(比如屏幕方向,键盘可用性和语言)。当这种变化发生时,机器人重新启动运行活动(的onDestroy()叫,随后的onCreate()

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

  • 要正确处理重新启动,但重要的是你的活动 通过正常的活动周期恢复其previous状态, 其中Android把的onSaveInstanceState()之前,它会破坏你的 活动让你可以将数据保存有关应用程序的状态。您 然后可以在的onCreate恢复状态() onRestoreInstanceState()
  • 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().

但是,您可能会遇到的情况是,重新启动您的应用程序和恢复显著的数据可能是昂贵的,并创建一个不好的用户体验。在这种情况下,您有两个选项:

  1. <一个href="http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject">Retain在配置变化对象

让你的活动重新启动时的配置变化,但携带状态的对象到你的活动的新实例。

Allow your activity to restart when a configuration changes, but carry a stateful Object to the new instance of your activity.

<一个href="http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange">Handle配置改变自己

prevent系统从在某些配置更改重新启动的活动,但收到一个回调时的配置做改变,使您可以手动根据需要更新您的活动。

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

编辑:按照您的需要只是改变了布局,我认为你必须使用 onConfigurationChanged()并做其中的变化。

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

这篇关于安卓:高效的屏幕旋转处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆