简体   繁体   English

Android和onSaveInstance以及还原

[英]Android and onSaveInstance and Restore

I have a big problem. 我有一个大问题。 I have a activity where I have two imageViews. 我有一个活动,其中有两个imageViews。 In onCreate I have List pictures where I have 10 pictures. 在onCreate中,我有列出图片,其中有10张图片。 Every time when I start this activity both imageViews get random picture from List and shows them. 每次启动此活动时,两个imageViews都会从List中获取随机图片并显示它们。 For example: When I start activity I can see car and pencil and when I start this activity once again I get elephant and book. 例如:当我开始活动时,我可以看到汽车和铅笔,当我再次开始此活动时,我得到了大象和书本。 Now this is my problem. 现在这是我的问题。 When I start activity and block my phone and after that unlock I get other pictures. 当我开始活动并阻止手机时,解锁后我会得到其他照片。 I think that onCreate is called again. 我认为onCreate被再次调用。 I create 我创造

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

pictureGameLeft and Right that is last images which is shows. pictureGameLeft和Right是最后显示的图像。 I write them after onCreate method. 我在onCreate方法之后编写它们。 How I can save last seen picture that if I lock and unlock phone I get the same pictures. 如何保存上次查看的图片,如果我锁定和解锁手机,我会得到相同的图片。 Maybe onPause or onStop need called? 也许onPause或onStop需要调用?

edit: now: 编辑:现在:

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

but I get NullPointerException. 但是我得到了NullPointerException。

I would save the current choice of pictures into the preferences at the very moment the decision is taken. 在做出决定的那一刻,我会将当前选择的图片保存到首选项中。 Than you don't have to fiddle with the live cycle methods. 比起您不必摆弄实时循环方法。

The decision when to take the choice from preference or to create a new (random) choice is easy. 决定何时从偏好中选择选项或创建新(随机)选项的决定很容易。 Just check whether 只要检查一下

savedInstanceState

of

public void onCreate (Bundle savedInstanceState)

is null. 一片空白。 If so, it is the first call. 如果是这样,这是第一个电话。 Subsequent reopen have a non-null value 后续的重新打开具有非空值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM