在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:
将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。
在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:
将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。
当前回答
与其试图阻止onCreate()被完全激发,不如尝试检查传递到事件中的Bundle savedInstanceState,看看它是否为null。
例如,如果我有一些逻辑应该在真正创建“活动”时运行,而不是在每次方向更改时运行,那么只有当savedInstanceState为空时,我才在onCreate()中运行该逻辑。
否则,我仍然希望布局按照方向正确重新绘制。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_list);
if(savedInstanceState == null){
setupCloudMessaging();
}
}
不确定这是否是最终答案,但这对我来说是有效的。
其他回答
您所描述的是默认行为。您必须通过添加以下内容自行检测和处理这些事件:
android:configChanges
然后是您想要处理的更改。因此,对于方向,您可以使用:
android:configChanges="orientation"
对于正在打开或关闭的键盘,您可以使用:
android:configChanges="keyboardHidden"
如果要同时处理这两种情况,只需使用pipe命令将它们分开,如:
android:configChanges="keyboardHidden|orientation"
这将在您调用的任何Activity中触发onConfigurationChanged方法。如果重写该方法,则可以传入新值。
希望这有帮助。
onConfigurationChanged is called when the screen rotates.
(onCreate is no longer called when the screen rotates due to manifest, see:
android:configChanges)
清单的哪个部分告诉它“不要调用onCreate()”?
而且谷歌的文档说要避免使用android:configChanges(除非作为最后手段)。但是,他们建议的替代方法都使用android:configChanges。
根据我的经验,模拟器总是在旋转时调用onCreate()。但我运行相同代码的1-2台设备……没有。(不知道为什么会有什么不同。)
我所做的。。。
在清单的活动部分中,添加了:
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();
}
将此代码添加到manifest.xml中。
这是你的活动。
<activity
....
..
android:configChanges="orientation|screenSize"/>
使用应用程序类
根据您在初始化中所做的操作,您可以考虑创建一个扩展Application的新类,并将初始化代码移动到该类中的重写onCreate方法中。
public class MyApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
// TODO Put your application initialization code here.
}
}
应用程序类中的onCreate仅在创建整个应用程序时调用,因此“活动”在方向或键盘可见性更改时重新启动不会触发它。
最好将这个类的实例公开为一个单例,并使用getter和setter公开正在初始化的应用程序变量。
注意:您需要在清单中指定要注册和使用的新Application类的名称:
<application
android:name="com.you.yourapp.MyApplicationClass"
对配置更改作出反应[UPDATE:自API 13以来,这已被弃用;请参阅推荐的替代方案]
作为另一种选择,您可以让应用程序监听可能导致重新启动的事件(如方向和键盘可见性更改),并在“活动”中处理这些事件。
首先将android:configChanges节点添加到“活动”的清单节点
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
或适用于Android 3.2(API级别13)及更高版本:
<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">
然后在“活动”中重写onConfigurationChanged方法,并调用setContentView以强制以新方向重新完成GUI布局。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}