-1
我是新來的android,並試圖在屏幕旋轉時保存一個布爾值,基本上我的問題是當前:當用戶在主要活動中按下「作弊」按鈕時,他被重定向到一個新的活動,他點擊一個按鈕「顯示答案」,布爾變量mIsCheater設置爲true,但是當屏幕旋轉時,該值變爲false。Android在旋轉時保存值
下面是代碼:
package com.example.macbookair.androidbooktutorials;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.macbookair.androidbooktutorials.Models.TrueFalse;
public class MainActivity extends AppCompatActivity {
private static final String KEY_INDEX = "index";
private static final String BOOLEAN_INDEX = "index";
private Button mTrueButton,mFalseButton,mCheatButton;
private ImageButton mNextButton;
private ImageButton mPrevButton;
private TextView mQuestionTextView;
private int mCurrentIndex = 0;
private boolean mIsCheater;
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_africa,false),
new TrueFalse(R.string.question_oceans,true),
new TrueFalse(R.string.question_mideast,false),
new TrueFalse(R.string.question_africa,true),
new TrueFalse(R.string.question_asia,true)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCheatButton = (Button) findViewById(R.id.cheat_button);
if(savedInstanceState != null) {
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
boolean theValue = savedInstanceState.getBoolean(BOOLEAN_INDEX);
Log.d("Is that true? -> ", mIsCheater + " ");
}
mTrueButton = (Button) findViewById(R.id.true_button);
mFalseButton = (Button) findViewById(R.id.false_button);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mNextButton = (ImageButton) findViewById(R.id.button_next);
mPrevButton = (ImageButton) findViewById(R.id.button_prev);
updateQuestion();
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE,answerIsTrue);
// startActivity(i);
startActivityForResult(i,0);
}
});
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex > 0) {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
} else {
Toast.makeText(MainActivity.this,R.string.message,Toast.LENGTH_SHORT).show();
}
}
});
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
updateQuestion();
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
}
});
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkedAnswer(true);
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkedAnswer(false);
}
});
}
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getmQuestion();
mQuestionTextView.setText(question);
}
private void checkedAnswer(boolean isTrue) {
boolean answer = mQuestionBank[mCurrentIndex].isTrueQuestion();
int messageId = 0;
if(mIsCheater) {
messageId = R.string.judgment_toast;
}
if(isTrue == answer) {
messageId = R.string.correct_toast;
} else {
messageId = R.string.incorrect_toast;
}
Toast.makeText(this,messageId,Toast.LENGTH_SHORT).show();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_INDEX,mCurrentIndex);
outState.putBoolean(BOOLEAN_INDEX, mIsCheater);
Log.d("What value is there? ->", mIsCheater + " ");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data == null) {
return;
}
mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN,false);
}
}
而且從第二個活動,以防萬一代碼:
package com.example.macbookair.androidbooktutorials;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends AppCompatActivity {
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
public static final String EXTRA_ANSWER_IS_TRUE = "com.example.macbookair.androidbooktutorials.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.example.macbookair.androidbooktutorials.answer_is_showed";
public static final String INDEX = "cheatIndex";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cheat_activity);
mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
if(savedInstanceState != null) {
String text = savedInstanceState.getString(INDEX,mAnswerTextView.toString());
mAnswerTextView.setText(text);
}
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
setAnswerShownResult(false);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
} else {
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(INDEX, mAnswerTextView.getText().toString());
}
private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
}
put
的數據我不知道是否有一個事件,火災時,檢測到一個方向變化... – csmckelvey你應該把數據放在'super.onSaveInstanceState(outState);' –
@ cricket_007是的情況下,得到它修復,謝謝:) –