Android和onSaveInstance和Restore
我有个大问题。我有一个活动,其中我有两个图像视图。在onCreate中,我列出了10张图片。每次我启动此活动时,两个ImageView都会从列表中随机获取图片并显示它们。例如:当我开始活动时,我可以看到汽车和铅笔,当我再次开始活动时,我可以看到大象和书。 这就是我的问题。当我开始活动并封锁我的手机,然后解锁,我会看到其他照片。我想再次调用onCreate。我创造Android和onSaveInstance和Restore,android,Android,我有个大问题。我有一个活动,其中我有两个图像视图。在onCreate中,我列出了10张图片。每次我启动此活动时,两个ImageView都会从列表中随机获取图片并显示它们。例如:当我开始活动时,我可以看到汽车和铅笔,当我再次开始活动时,我可以看到大象和书。 这就是我的问题。当我开始活动并封锁我的手机,然后解锁,我会看到其他照片。我想再次调用onCreate。我创造 @Override protected void onSaveInstanceState(Bundle savedInstan
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null) {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair().getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair().getImageRight()));
}
}
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
图片左侧和右侧是最后显示的图像。
我在onCreate方法之后编写它们。
如何保存上次看到的图片,如果我锁定和解锁手机,我会得到相同的图片。可能是暂停或暂停需要呼叫
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
编辑:
现在:
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
但是我得到了NullPointerException。我会在做出决定的那一刻将当前选择的图片保存到首选项中。
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
这样你就不必去摆弄生命周期方法了
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
决定何时从偏好中选择或创建新的(随机)选择是很容易的。检查一下
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
savedInstanceState
的
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}
是空的。如果是,这是第一个电话。随后的重新打开有一个非空值现在我得到java.lang.RuntimeException:无法暂停活动
if (savedInstanceState == null) {
game = new Game();
game.initGame();
addElements(result);
game.getCurrentLevel().nextPair();
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
mRedrawHandler.sleep(1000);
}
else {
pictureGameLeft.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair(
.getImageLeft()));
pictureGameRight.setImageResource(shapesPictures.get(game.getCurrentLevel().getCurrentPair()
.getImageRight()));
}