如何在onSaveInstanceState中保存按钮的isSelected()属性,然后使用onRestoreInstanceState恢复它?

时间:2014-08-07 11:45:19

标签: java android

我想在onSaveInstanceState中保存按钮的isSelected()属性,然后在旋转设备时使用onRestoreInstanceState恢复它。这是一个骰子游戏,我有三种骰子,投掷活动骰子(白色),选择任何骰子(红色),选择和得分停用骰子后是黑色。

一旦我投掷,选择,得分,并旋转手机,活动骰子将恢复,停用的骰子也可以恢复,但我想恢复我可能已点击的骰子(但没有得分,如在旋转手机之前,得分将使他们停用黑色骰子。

//Change the dice icon on selection
private void setSelectDice(ImageButton imgBtn, int face, boolean stat) {

    imgBtn.setSelected(stat);
    if (stat) {
        imgBtn.setImageResource(imgSelectedDice[face-1]);
    }  
}

//save instance
public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        savedInstanceState.putIntArray("diceValues", diceFaces);
        savedInstanceState.putInt("totalScore", diceAction.getTotalScore());

        //dice action is a handler class that has certain dice operation
        //this part is working fine where i want to restore the active dices that i have thrown
        List<Integer> activeDiceList = diceAction.getCurrentDice();
        int[] activeDice = new int[activeDiceList.size()];
        for (int i = 0; i < activeDiceList.size(); i++) {
            activeDice[i] = activeDiceList.get(i);
        }
        savedInstanceState.putIntArray("activeDice", activeDice);

        //Here i got stuck, I am trying to use the same techniques above to create a list  of selected dice
        ArrayList<Integer> selDiceList = new ArrayList<Integer>();
        int[] selDice = new int[selDiceList.size()];
        for (int i = 0; i < imgBtnDice.length; i++) {
            if (imgBtnDice[i].isSelected()) {
                selDiceList.add(i);
                selDice[i] = selDiceList.get(i);
            }
        }
        savedInstanceState.putIntArray("selectedDice", selDice);

    }


@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);

        diceFaces = savedInstanceState.getIntArray("diceValues");

        //Array of active dice, working fine
        int[] activeDice = savedInstanceState.getIntArray("activeDice");
        //Array of selected dice, ???
        int[] selDice = savedInstanceState.getIntArray("selectedDice");

        boolean isThrowableStat = savedInstanceState.getBoolean("throwStat");
        boolean isSavableStat = savedInstanceState.getBoolean("saveStat");
        diceAction = new DiceHandler(diceFaces, activeDice, isThrowableStat, isSavableStat);

        diceAction.setTotalScore(savedInstanceState.getInt("totalScore"));
        // Creating a list of current dice and traversing it to set up active dice image and deactivated dice image, working fine

        List<Integer> activeDiceList = diceAction.getCurrentDice();
        for (int i = 0; i < diceFaces.length; i++) {
            imgBtnDice[i].setImageResource(imgActiveDice[diceFaces[i] - 1]);    
            setDeactiveDice(imgBtnDice[i], diceFaces[i], activeDiceList.contains(i));

        }

        //trying to do the same technique as above but not working
        List<Integer> selectedDiceList = new ArrayList<Integer>();
        for (int i = 0; i < diceFaces.length; i++) {
            setSelectDice(imgBtnDice[i], diceFaces[i], selectedDiceList.contains(i));
        }

        tvRound.setText(diceAction.getRoundNr().toString());
        tvFinalScore.setText(diceAction.getTotalScore().toString());
        tvTurnScore.setText(diceAction.getRoundScore().toString());

    }

非常感谢帮助。有没有其他方式(比上面提到的)我可以做到这一点?

2 个答案:

答案 0 :(得分:0)

这里有一些知识问题湖,onRestoreInstanceState方法在onCreate方法之后调用,因此您必须检查onCreate中的所有保存状态。见下面的代码

onCreate(Bundle b){
.....
.....
if(b!=null){
.....
.....     
//paste here your onRestoreInstanceState code thats it and Remove onRestoreInstanceState method no need it.
}
}

多数民众赞成......

答案 1 :(得分:0)

尝试过尝试将 android:configChanges 节点添加到Activity的清单xml中

android:configChanges="keyboardHidden|orientation|screenSize"