Mik*_*ike 2 java android rotation
我正在开发一个Android应用程序,现在我限制用户只对所有活动使用水平视图.
我希望能够为用户提供旋转屏幕的选项,但是当我这样做时,活动从头开始而不是保持不变.
知道如何在旋转屏幕时保存状态吗?
首先,您必须在Activity中覆盖onConfigurationChanged(Configuration).
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
您还必须编辑清单文件中的相应元素以包含android:configChanges只需看下面的代码:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
Run Code Online (Sandbox Code Playgroud)
我也喜欢Marko的想法,但它根本没有效率.这样,我们就不必调用onCreate/onStartup/etc. 我们一直做一个简单的旋转.无需从头开始"重建"基础架构(例如获取视图,......)