如何使用onSavedInstanceState()在更改方向或切换屏幕时保存更改的图像

时间:2015-10-20 03:30:55

标签: java android android-activity orientation

我已经创建了一个几乎完成的掷骰子应用程序,但是当改变屏幕的方向时,我无法保存骰子图像。我一直在使用onSavedInstanceState()方法,并且无法弄清楚如何实现这一点。我还注意到,当我切换到新的活动屏幕("如何播放"屏幕)然后返回主屏幕时,即使在更改时保存了以前的活动也没有保存取向。

public class MainActivity extends Activity
{
    // declarations
    private Button button;
    private ImageView dice1Image;
    private ImageView dice2Image;
    private TextView roll;
    private TextView point;
    private TextView winTotal;
    private TextView lostTotal;
    private TextView results;

    int winNumber1 = 7;
    int winNumber2 = 11;
    int loseNumber1 = 2;
    int loseNumber2 = 3;
    int loseNumber3 = 12;

    int rounds = 0;
    int rollTotal = 0;
    int pointValue = 0;
    int winsCounter = 0;
    int loseCounter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

        dice1Image = (ImageView) findViewById(R.id.imageViewDie01);
        dice2Image = (ImageView) findViewById(R.id.imageViewDie02);
        roll = (TextView) findViewById(R.id.textViewRollTotal);
        point = (TextView) findViewById(R.id.textViewPoint);
        winTotal = (TextView) findViewById(R.id.textViewWonTotal);
        lostTotal = (TextView) findViewById(R.id.textViewLostTotal);
        results = (TextView) findViewById(R.id.results);
        results.setVisibility(View.GONE);
        // Locate the button in activity_main.xml
        button = (Button) findViewById(R.id.howToPlayButton);

        if (savedInstanceState != null) {
            int pointValue = savedInstanceState.getInt("POINT_VALUE");
            int rollTotal = savedInstanceState.getInt("ROLL_TOTAL");
            int winsCounter = savedInstanceState.getInt("WINS_COUNTER");
            int loseCounter = savedInstanceState.getInt("LOSE_COUNTER");
            String result = savedInstanceState.getString("RESULTS");
            int dice1 =  savedInstanceState.getInt("DIE_1", R.id.imageViewDie01);
            int dice2 =  savedInstanceState.getInt("DIE_2", R.id.imageViewDie02);

            roll.setText(String.valueOf(rollTotal));
            point.setText(String.valueOf(pointValue));
            winTotal.setText(String.valueOf(winsCounter));
            lostTotal.setText(String.valueOf(loseCounter));
            results.setText(String.valueOf(result));
        }

我意识到永远不会使用dice1dice2变量。那是我被困的地方。我不确定保存图像的最佳方法。以下是onSavedInstanceState()onRestoreInstanceState()方法:

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        // Save UI state changes to the savedInstanceState.
        // This bundle will be passed to onCreate if the process is
        // killed and restarted.
        String result = results.getText().toString();
        savedInstanceState.putInt("POINT_VALUE", pointValue);
        savedInstanceState.putInt("ROLL_TOTAL", rollTotal);
        savedInstanceState.putInt("WINS_COUNTER", winsCounter);
        savedInstanceState.putInt("LOSE_COUNTER", loseCounter);
        savedInstanceState.putString("RESULTS", result);
        savedInstanceState.putInt("DIE_1", d1);
        savedInstanceState.putInt("DIE_2", d2);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        int pointValue = savedInstanceState.getInt("POINT_VALUE");
        int rollTotal = savedInstanceState.getInt("ROLL_TOTAL");
        int winsCounter = savedInstanceState.getInt("WINS_COUNTER");
        int loseCounter = savedInstanceState.getInt("LOSE_COUNTER");
        String result = savedInstanceState.getString("RESULTS");
        int dice1 =  savedInstanceState.getInt("DIE_1");
        int dice2 =  savedInstanceState.getInt("DIE_2");
    }

d1d2变量用于我的rollDice()方法,该方法为骰子图片设置了这样的值:

// set values for dice images
        if(dice1 == 1) {
            dice1Image.setImageResource(R.drawable.die1);
            d1 = 1;
        }....

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

因此,我没有尝试保存图像,而是根据骰子滚动保存的整数重新编写它们。

我的onCreate()内部:

        if (savedInstanceState != null) {
            onRestoreInstanceState(savedInstanceState);
        }

这是我的onSavedInstanceState()

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        // Save UI state changes to the savedInstanceState.
        // This bundle will be passed to onCreate if the process is
        // killed and restarted.
        savedInstanceState.putInt("POINT_VALUE", pointValue);
        savedInstanceState.putInt("ROLL_TOTAL", rollTotal);
        savedInstanceState.putInt("WINS_COUNTER", winsCounter);
        savedInstanceState.putInt("LOSE_COUNTER", loseCounter);
        savedInstanceState.putInt("DIE_1", d1);
        savedInstanceState.putInt("DIE_2", d2);
    } // end onSavedInstanceState

这是我的onRestoreInstanceState()

@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        pointValue = savedInstanceState.getInt("POINT_VALUE");
        rollTotal = savedInstanceState.getInt("ROLL_TOTAL");
        winsCounter = savedInstanceState.getInt("WINS_COUNTER");
        loseCounter = savedInstanceState.getInt("LOSE_COUNTER");
        d1 =  savedInstanceState.getInt("DIE_1");
        d2 =  savedInstanceState.getInt("DIE_2");

        roll.setText(String.valueOf(rollTotal));
        point.setText(String.valueOf(pointValue));
        winTotal.setText(String.valueOf(winsCounter));
        lostTotal.setText(String.valueOf(loseCounter));

        if(d1 == 1) {
            dice1Image.setImageResource(R.drawable.die1);
        }
        else if(d1 == 2) {
            dice1Image.setImageResource(R.drawable.die2);
        }
        else if(d1 == 3) {
            dice1Image.setImageResource(R.drawable.die3);
        }
        else if(d1 == 4) {
            dice1Image.setImageResource(R.drawable.die4);
        }
        else if(d1 == 5) {
            dice1Image.setImageResource(R.drawable.die5);
        }
        else if(d1 == 6) {
            dice1Image.setImageResource(R.drawable.die6);
        }

        if(d2 == 1) {
            dice2Image.setImageResource(R.drawable.die1);
        }
        else if(d2 == 2) {
            dice2Image.setImageResource(R.drawable.die2);
        }
        else if(d2 == 3) {
            dice2Image.setImageResource(R.drawable.die3);
        }
        else if(d2 == 4) {
            dice2Image.setImageResource(R.drawable.die4);
        }
        else if(d2 == 5) {
            dice2Image.setImageResource(R.drawable.die5);
        }
        else if(d2 == 6) {
            dice2Image.setImageResource(R.drawable.die6);
        }
    } // end onRestoreInstanceState method

通过为每个可绘制图像重新分配值,然后使用保存的骰子值确定应显示哪个图像,可以在旋转屏幕时将图像还原为默认图像。不确定这是否是最佳解决方案,但它解决了我的问题。