在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:
将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。
在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:
将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。
当前回答
我所做的。。。
在清单的活动部分中,添加了:
android:configChanges="keyboardHidden|orientation"
在活动代码中,实现:
//used in onCreate() and onConfigurationChanged() to set up the UI elements
public void InitializeUI()
{
//get views from ID's
this.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);
//etc... hook up click listeners, whatever you need from the Views
}
//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InitializeUI();
}
//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
InitializeUI();
}
其他回答
有很多方法可以做到这一点:
在创建时使用标志在清单文件中添加以下行
> android:screenOrientation=“纵向”
在onCreate()中
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
使用onConfigurationChanged()方法
在清单文件中,为Android 3.2及更高版本添加以下行/
android:configChanges="keyboardHidden|orientation" / android:configChanges="keyboardHidden|orientation|screenSize"
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//Handle rotation from landscape to portrait mode here
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//Handle rotation from portrait to landscape mode here
}
}
使用onSaveInstanceState()@覆盖public void onSaveInstanceState(捆绑outState){/在此处保存要还原的数据示例:outState.putLong(“time_state”,time),时间是一个长变量/super.onSaveInstanceState(outState);}
和恢复
@覆盖protected void onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);
if(savedInstanceState!= null){
/*When rotation occurs
Example : time = savedInstanceState.getLong("time_state", 0); */
} else {
//When onCreate is called for the first time
}
}
要在Android清单中进行的更改包括:
android:configChanges="keyboardHidden|orientation"
要在活动中添加的内容包括:
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();
}
}
每次旋转屏幕时,打开的活动都会完成,并再次调用onCreate()。
1.当屏幕旋转时,您可以做一件事来保存活动状态,这样,当再次调用活动的onCreate()时,您就可以恢复所有旧的内容。参考此链接
2.如果要阻止重新启动活动,只需在manifest.xml文件中放置以下行。
<activity android:name=".Youractivity"
android:configChanges="orientation|screenSize"/>
将以下代码放入Manifest.xml中的<activity>标记中:
android:configChanges="screenLayout|screenSize|orientation"
您还可以考虑使用Android平台的跨方向更改持久化数据的方式:onRetainNonConfigurationInstance()和getLastNonConfigurationInstance)。
这允许您在配置更改中保存数据,例如您可能从服务器获取的信息或在onCreate中或之后计算的其他信息,同时还允许Android使用xml文件重新布局“活动”,以适应当前使用的方向。
请看这里或这里。
应该注意的是,这些方法现在已经被弃用(尽管比上面大多数解决方案所建议的自己处理方向改变更灵活),建议每个人都切换到Fragments,而不是在每个想要保留的Fragment上使用setRetainInstance(true)。