在更改屏幕显示方向,新的活动创建屏幕、方向

2023-09-12 05:26:42 作者:纯种大傻逼°

我是比较新的到Android,但已经取得了在过去一年相当多的应用程序所以请原谅我。

I'm relatively new to Android but have made quite a few apps over the past year so forgive me.

我知道,当你在设备上运行的应用程序,并通过旋转设备所显示的活动是完全重新改变屏幕的方向。我去YouTube应用程序(我用的Nexus 7W /安卓4.2.2),并播放一些视频,然后翻转屏幕改变方向。视频继续播放,一切都重新大小相应地......这怎么可能,如果该活动是完全重现?

I know that when you are running an app on a device and change the screen orientation by turning the device the activity that is showing is completely recreated. I go to the youtube app (I am using Nexus 7 w/ Android 4.2.2) and play some video and then flip the screen to change orientation. The video keeps playing and everything is re-sized accordingly... How is this possible if the activity is completely recreated?

感谢你。

推荐答案

在你的Andr​​oidManifest.xml中添加,安卓configChanges,这将避免对方向变化重新创建活动。我希望你的横向和纵向模式具有相同的布局。

In your AndroidManifest.xml add, android:configChanges and this would avoid the activity being re-created on orientation change. I hope your landscape and portrait mode has the same layout.

    <activity android:label="Your Activity Name" 
    android:configChanges="keyboardHidden|orientation|screenSize"
      android:name="com.yourpackage">

要添加更多的这一点,我建议你看看的onPause() onResume()方法,如果你正在玩的片段,然后对手表的onSaveInstanceState onRetainInstanceState ,而不是应用XML变化 活动是由设计破坏。

To add more to this, i would suggest you to look at the onPause() and onResume() methods and if you are playing with Fragments then have a watch on onSaveInstanceState and onRetainInstanceState rather than applying the xml changes as "Activity is destroyed by design."

http://developer.android.com/training/basics/activity-lifecycle/index.html