gpt4 book ai didi

android - 在双布局 Activity 中在屏幕上旋转保存实例

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:43 25 4
gpt4 key购买 nike

我的 Activity 有一个 ImageView。它从拍照 Action 中获取图像。此 Activity 有两种布局。 Two layouts

在 list 中,有:

The manifest

我的问题是,如果我删除

configChanges="orientation|keyboardHidden|screenSize"

数据不会保存,但如果不保存,则不会加载 form_layout.xml (land)。 screenRotate 上加载的是默认横向布局。

如何解决我的问题,即在使用保存的数据旋转后加载新的 xml 布局?

更新

我不想留下

configChanges="orientation|keyboardHidden|screenSize"

最佳答案

如果要保持configChanges="orientation|keyboardHidden|screenSize"需要再次手动调用setContentView

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

//Get last state
String text = et1.getText().toString();

//Create new view according to layout
setContentView(R.layout.activity_login);

// Restore last state
et1 = (EditText) findViewById(R.id.et1);
et1.setText(text);
}

否则最好的办法是将数据保存到 Bundle onSaveInstanceState 并在 onCreate 中恢复它

请参阅此处 http://developer.android.com/training/basics/activity-lifecycle/recreating.html

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);
}

@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 - 在双布局 Activity 中在屏幕上旋转保存实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30394830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com