Android 网格布局机器人
我目前正在开发一款Android应用程序,当我使用GridLayout并将平板电脑置于横向模式时,该布局会被重新加载,我会丢失所有变量Android 网格布局机器人,android,layout,Android,Layout,我目前正在开发一款Android应用程序,当我使用GridLayout并将平板电脑置于横向模式时,该布局会被重新加载,我会丢失所有变量 static final String STATE_SCORE = "playerScore"; static final String STATE_LEVEL = "playerLevel"; ... @Override public void onSaveInstanceState(Bundle savedInstanceState) { // S
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
这个布局有什么特别之处吗?没有,没什么特别的。这就是安卓系统的工作原理。 但是,如果您想在每次配置更改时停止加载活动,可以将此代码放入
Manifest.xml
文件中
<activity>
android:configChanges="orientation|screenSize"
...
</activity>
不,没什么特别的。这就是安卓系统的工作原理。 但是,如果您想在每次配置更改时停止加载活动,可以将此代码放入
Manifest.xml
文件中
<activity>
android:configChanges="orientation|screenSize"
...
</activity>
当您的方向发生变化时,android系统会重新创建活动以加载该特定方向的变化。可以防止重新加载,也可以重新设计每个方向的布局。要更改布局,必须创建单独的布局文件,并且必须在onCreate中进行设置 例: 或者就像我说的,你必须阻止重新创建活动。即使你可以试一试,这也不是一个好的选择。为此,您必须在您的清单中设置它
<activity
android:name="MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
当您的方向发生变化时,android系统会重新创建活动以加载该特定方向的变化。可以防止重新加载,也可以重新设计每个方向的布局。要更改布局,必须创建单独的布局文件,并且必须在onCreate中进行设置 例: 或者就像我说的,你必须阻止重新创建活动。即使你可以试一试,这也不是一个好的选择。为此,您必须在您的清单中设置它
<activity
android:name="MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
当您旋转android设备时,当前活动和其中的所有内容都将被销毁,活动将再次创建。要保留变量,请覆盖onSaveInstanceState(在活动销毁之前调用)并存储变量
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
然后在onCreate方法中添加以下代码,以检查是否存在活动可能已销毁时的数据。如果有,它将把数据放入适当的变量中,这样您就可以继续使用活动,就好像什么都没有改变一样
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
当您旋转android设备时,当前活动和其中的所有内容都将被销毁,活动将再次创建。要保留变量,请覆盖onSaveInstanceState(在活动销毁之前调用)并存储变量
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
然后在onCreate方法中添加以下代码,以检查是否存在活动可能已销毁时的数据。如果有,它将把数据放入适当的变量中,这样您就可以继续使用活动,就好像什么都没有改变一样
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
您说的是一种解决方案,而不是说明原因和其他解决方案。这可能有效,但OP可能不理解使用代码的原因。此外,建议在API级别3的配置更改中添加屏幕大小。1@VividVervet你是对的,我正在学习如何回答stackoverflow,我会注意你的建议,我也会更新我的答案。谢谢。那太棒了:)你说的是一个解决方案,而不是说明原因和其他解决方案。这可能有效,但OP可能不理解使用代码的原因。此外,建议在API级别3的配置更改中添加屏幕大小。1@VividVervet你是对的,我正在学习如何回答stackoverflow,我会注意你的建议,我也会更新我的答案。谢谢。这真是太棒了:)