防止在方向改变时重新启动活动 [英] prevent activity restarting when orientation changes
问题描述
我是 android 开发的新手.我有单独的纵向和横向模式屏幕.当我改变我的方向时,相应的屏幕被加载并且活动重新启动.现在我不希望我的活动在我改变方向时重新启动,但应该加载其相应的屏幕(axml).
I am new to android development .I have separate screens for portrait and landscape mode.When i change my orientation corresponding screen gets loaded and activity restarts . Now i do not want my activity to restart when i change the orientation but should load its corresponding screen(axml).
我试过了
[活动(标签 ="MyActivity",ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]
[Activity (Label = "MyActivity",ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]
上述行停止重新启动活动,但它加载了相同的屏幕(axml).请建议.谢谢
the above line stops activity getting restarted but it loads the same screen(axml). Please suggest . thanks
推荐答案
将此代码写入您的活动
Write this code in your activity
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscapeView);
} else {
setContentView(R.layout.portraitView);
}
}
并在您的清单文件中添加这一行
And also add this line in your Manifest file
android:configChanges="orientation|keyboardHidden|screenSize"
所以这将处理这两件事,它不会重新启动您的活动,而是会根据您的方向更改加载布局.
So this will handle both things, it will not restart your activity and will load the layout as per your orientation changes.
这篇关于防止在方向改变时重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!