我找不到一个简单的解决方案来避免我的 ImageSwitcher 在我旋转屏幕时更改显示的 ImageResource。我尝试并尝试在 onSaveInstanceState() 和 onRestoreInstanceState() 中编写不同的东西,但我找不到任何有用的东西。
我怎样才能实现这样的事情(纯伪代码):
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
int picNum = imageSwitcher.getImageResource();
savedInstanceState.putInt("picNum", picNum);
// etc.
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
Int picNum = savedInstanceState.getString("picNum");
imageSwitcher.setImageResource(picNum);
}
我不确切知道显示的图像名称,所以我必须每次都找到它(每次 ImageSwitcher 显示来自“原始”文件夹的随机图像)。我正在使用的代码的简化版本是这样的:
rawClass = R.raw.class;
fields = rawClass.getFields();
rand = new Random();
rndInt = rand.nextInt(fields.length);
resID = fields[rndInt].getInt(rawClass);
imageSwitcher.setImageResource(resID);
先感谢您