旋转时保存状态

时间:2014-09-19 09:36:23

标签: java android

我有一个应用程序,主要活动显示一个问题,并有真正的按钮假按钮和作弊按钮 当用户按下正确的选项时,监听器将Toast Correct ..否则侦听器将Toast Incorrect

现在,当我们按下作弊按钮时,会启动新的活动并且有一个文本视图“你确定要作弊吗?”,另一个空白的文本视图和一个显示答案按钮..

当用户按下显示应答按钮时,空白文本视图将设置为问题的答案(如主要活动中所述)

当用户返回时,onClickListeners上的true和false按钮现在设置为“敬酒是错误的”

我声明了一个设置为false的布尔值,布尔值设置为True并且我将它保存在包中,这样当用户旋转屏幕时,变量的值不会被onCreate覆盖(。 。)方法被召回..

我尝试在eclipse中使用断点进行调试,但是在主要活动中仍然没有覆盖该值“作弊是错误的”并不显示,它显示正确或不正确..

QuizActivity:启动器活动 CheatActivity:在onCreate上启动的活动 TrueFalse活动:使用字段(问题和答案)创建对象数组

以下是代码:QuizActivity.java(Launcher活动)

package com.mhrsolanki2020.geoquiz;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {

private Button mTrueButton, mFalseButton, mNextButton, mCheatButton;
private TextView mQuestionTextView;
private static final String KEY_INDEX = "index";
private static final String TAG = "QuizActivity";
private TrueFalse[] mQuestionBank = new TrueFalse[] {
        new TrueFalse(R.string.question_oceans, true),
        new TrueFalse(R.string.question_mideast, false),
        new TrueFalse(R.string.question_africa, false),
        new TrueFalse(R.string.question_americas, true),
        new TrueFalse(R.string.question_asia, true) };

private int mCurrentIndex = 0;
private boolean mIsCheater;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            checkAnswer(true);
        }
    });

    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            checkAnswer(false);

        }
    });

    mNextButton = (Button) findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;

            updateQuestion();
            mIsCheater=false;

        }
    });

    mCheatButton = (Button) findViewById(R.id.cheat_button);
    mCheatButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(QuizActivity.this, CheatActivity.class);
            boolean answer = mQuestionBank[mCurrentIndex].isTrueQuestion();
            i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answer);
            startActivityForResult(i, 0);
        }
    });

    if (savedInstanceState != null)
        mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);

    updateQuestion();

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}

private void updateQuestion() {

    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);

}

private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
    int messageResId = 0;
    if (mIsCheater) {
        messageResId = R.string.judgement_toast;
    } else {
        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
    }
        Toast.makeText(this, messageResId, Toast.LENGTH_LONG).show();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null) {
        return;
    } else {
        mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN,
                false);
        Log.d(TAG, "Value of mIsCheater : " + mIsCheater);
    }
}
}

以下是代码:CheatActivity.java

package com.mhrsolanki2020.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity {

public static final String EXTRA_ANSWER_IS_TRUE = "com.mhrsolanki2020.geoquiz.anwer_is_true ";
public static final String EXTRA_ANSWER_SHOWN = "com.mhrsolanki2020.geoquiz.answer_shown";
public static final String EXTRA_IS_ANSWER_SHOWN = "com.mhrsolanki2020.geoquiz.answer_is_shown";
boolean mCorrectAnswer;
private TextView mAnswerTextView;
private Button mShowAnswer;
private Boolean mIsAnswerShown;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);
    mCorrectAnswer = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,
            false);
    mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
    mAnswerTextView = (TextView) findViewById(R.id.answerTextView);

    if (savedInstanceState != null) {
        mIsAnswerShown = savedInstanceState.getBoolean(
                EXTRA_IS_ANSWER_SHOWN, false);
    } else {
        mIsAnswerShown = false;
    }

    mShowAnswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mCorrectAnswer) {
                mAnswerTextView.setText(R.string.true_button);

            } else {
                mAnswerTextView.setText(R.string.false_button);
            }
            setAnswerShownResult(true);
        }
    });

}

public void setAnswerShownResult(boolean isAnswerShown) {
    Intent data = new Intent();
    mIsAnswerShown = isAnswerShown;

    data.putExtra(EXTRA_ANSWER_SHOWN, mIsAnswerShown);
    setResult(RESULT_OK, data);
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putBoolean(EXTRA_IS_ANSWER_SHOWN, mIsAnswerShown);

}

}

以下是代码:TrueFalse.java     包com.mhrsolanki2020.geoquiz;

public class TrueFalse {
private int mQuestion;

private boolean mTrueQuestion;

public TrueFalse(int question, boolean trueQuestion) {
    mQuestion = question;
    mTrueQuestion = trueQuestion;

}

public int getQuestion() {
    return mQuestion;
}

public void setQuestion(int question) {
    mQuestion = question;
}

public boolean isTrueQuestion() {
    return mTrueQuestion;
}

public void setTrueQuestion(boolean trueQuestion) {
    mTrueQuestion = trueQuestion;
}
}

1 个答案:

答案 0 :(得分:0)

http://developer.android.com/guide/topics/resources/runtime-changes.html点击此链接

对于方向支持,请在manifest.xml中执行此操作

      <activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

在Class文件中,覆盖onSaveInstanceState(Bundle outState)。