суть приложения в том что есть 6 вопросов 2 кнопки тру фолс. 2 кнопки на след вопрос и назад. при ответе на вопрос кнопки(тру фолс) становятся недоступны. когда будут ответы на все вопросы всплывает тоаст с % правильных ответов. в портретном все работает. я сделал hashset в который записываю индексы массива с вопросами. и проверяю после каждого ответа есть ли пустые значения в массиве, если нет то всплывает тоаст с %. При смене ориентации кнопки снова активны. я хотел передать hashset(с именем haveAnswers) с индексами в альбомную активити, чтобы и в альбомной ориентации было известно на какие вопросы были даны ответы. Пробовал по примерам сделать это с помощью Parcelable, не получается. во всех примерах описывается передача интента в другую активити и другого класса имплементирующего Parcelable. а тут получается всё в одном классе. Подскажите пожалуйста, что делать?
P.S. Далее привожу работоспособную версию кода:
public class QuizActivity extends AppCompatActivity {
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private static final String KEY_BUTTONS = "keyButtons";
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mPrevButton;
private ImageButton mNextButton;
private TextView mQuestionTextView;
private boolean f;
private int correctAnswers = 0;
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true)
};
private HashSet<Integer> haveAnswers = new HashSet<>(mQuestionBank.length);
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
if (savedInstanceState != null) {
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
}
mQuestionTextView = findViewById(R.id.question_text_view);
updateQuestion();
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextUpdate();
checkButtons();
}
});
mTrueButton = findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mPrevButton = findViewById(R.id.previous_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex > 0) {
mCurrentIndex--;
}
checkButtons();
updateQuestion();
}
});
mNextButton = findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextUpdate();
checkButtons();
}
});
if (savedInstanceState != null) {
mTrueButton.setEnabled(savedInstanceState.getBoolean(KEY_BUTTONS, true));
mFalseButton.setEnabled(savedInstanceState.getBoolean(KEY_BUTTONS, true));
}
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "on Start() called");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
outState.putInt(KEY_INDEX, mCurrentIndex);
outState.putBoolean(KEY_BUTTONS, mTrueButton.isEnabled());
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void nextUpdate() {
if (mCurrentIndex < 5) {
mCurrentIndex++;
}
updateQuestion();
}
private void checkButtons() {
if (!haveAnswers.contains(mCurrentIndex)) {
mTrueButton.setEnabled(true);
mFalseButton.setEnabled(true);
} else {
mTrueButton.setEnabled(false);
mFalseButton.setEnabled(false);
}
}
private void checkAnswer(boolean userPressedTrue) {
mTrueButton.setEnabled(false);
mFalseButton.setEnabled(false);
haveAnswers.add(mCurrentIndex);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
correctAnswers++;
} else {
messageResId = R.string.incorrect_toast;
}
//Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
Toast t = Toast.makeText(this, messageResId, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP, 0 ,200);
t.show();
if (haveAnswers.size() == mQuestionBank.length) {
Toast.makeText(this, String.format("You have %d%%", correctAnswers*100/mQuestionBank.length), Toast.LENGTH_LONG).show();
}
}
}
bundle
?