我使用 onSaveInstanceState 和 onRestoreInstanceState 在重启活动后保存数组中的图片位置。但它不起作用 - 重新启动活动数组重新运行后,问题是什么?
int[] array_images = {
R.drawable.p_orange,
R.drawable.p_banana,
R.drawable.p_apple,
};
int index = -1;
private void updateImage()
{
index++;
if(index >= array_images.length)
index = 0;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(array_images[index]);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putIntArray("array_images", array_images);
super.onSaveInstanceState(outState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
array_images = savedInstanceState.getIntArray("array_images");
}
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
finish();
startActivity(getIntent());
updateImage();
}
});
答案 0 :(得分:2)
在onRestoreInstanceState(Bundle savedInstanceState)
中,只会恢复值,但会重新创建您的视图。因此,您必须在onRestoreInstanceState(Bundle savedInstanceState)
中恢复值后将所有值设置为视图,或者甚至可以在onCreate()
中执行此操作,您也将在onCreate()
中收到已保存的包。