Good day. Faced such a problem (I practice on the Android book. Programming for professionals. 3rd edition - stuck on the last assignments of chapter 5): lower class, which allows the user to peep the correct answer to a simple question. After viewing the tooltip when you go back to the questions and clicking on the correct answer, the user will see the inscription "Cheat is not good." The problem is that in this class you need to save the result - whether the user has spied the answer so that when the phone screen is turned over, this indicator will not be reset. I do not understand how to do this. What exactly should be saved? In place of the code -> (________) I tried to substitute the value of mAnswerIsTrue - but this did not give the expected results. I think that you need to save the result of executing the wasAnswerShown method, but this is only a guess. Thank you in advance)
public class CheatActivity extends AppCompatActivity { //ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ° TAG (ΠΈΡΡΠΎΡΠ½ΠΈΠΊ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ) = "ΠΈΠΌΡ ΠΊΠ»Π°ΡΡΠ°" public static final String TAG = "CheatActivity"; //ΠΊΠ»ΡΡ Π΄Π»Ρ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ-ΠΈΠ½ΡΠ΅Π½ΡΠ° ΠΊΠΎΡΠΎΡΡΠΉ CheatActivity ΡΠΈΡΠ°Π΅Ρ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ (Π² ΠΌΠ΅ΡΠΎΠ΄Π΅ newIntent) private static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true"; //ΠΊΠ»ΡΡ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π΄Π»Ρ Π½Π°Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° private static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown"; private boolean mAnswerIsTrue; private TextView mAnswerTextView; private Button mShowAnswerButton; //ΠΈΠ½ΠΊΠ°ΠΏΡΡΠ»ΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π΄Π»Ρ Π²ΠΊΠ»ΡΡΠ΅Π½ΠΈΡ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π² ΠΈΠ½ΡΠ΅Π½Ρ //ΠΎΠ½ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠΎΠ·Π΄Π°ΡΡ ΠΎΠ±ΡΠ΅ΠΊΡ Intent, Π½Π°ΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡΠΌΠΈ, Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΡΠΌΠΈ Π΄Π»Ρ CheatActivity // boolean answerIsTrue - Π²ΡΡΡΡΠΏΠ°Π΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ Π² Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ // (Π²ΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π² ΠΈΠ½ΡΠ΅Π½Ρ - ΠΈΠ½ΡΠ΅Π½Ρ.Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅(ΠΊΠ»ΡΡ,Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅)) public static Intent newIntent (Context packageContext, boolean answerIsTrue){ Intent intent = new Intent(packageContext, CheatActivity.class); //Π΄Π°Π»Π΅Π΅ ΠΈΠ½ΡΠ΅Π½Ρ.Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ (ΠΊΠ»ΡΡ, Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅) intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue); return intent; } //Π΄Π΅ΠΊΠΎΠ΄ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΈΠ½ΡΠ΅Π½ΡΠ° ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° (ΠΈΠ· ΠΌΠ΅ΡΠΎΠ΄Π° setAnswerShownResult) public static boolean wasAnswerShown (Intent result){ return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate(Bundle) called");// <- ΡΡΠ° ΡΡΡΠΎΠΊΠ° ΡΠ΅Π³ΠΈΡΡΡΠΈΡΡΠ΅Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ (Log.d), // ^(ΠΈΡΡΠΎΡΠ½ΠΈΠΊ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ,"ΠΊΠ°ΠΊΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Π²ΡΠ²Π΅ΡΡΠΈ") setContentView(R.layout.activity_cheat); if (savedInstanceState != null){ ___________ = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, ___________); } // ΠΌΠ΅ΡΠΎΠ΄ Π΄Π»Ρ ΡΡΠ΅Π½ΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΈΠ· Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ + ΡΠΎΡ
ΡΠ°Π½Π΅Π½ΠΈΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ mAnswerIsTrue mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false); mAnswerTextView = (TextView) findViewById(R.id.answer_text_view); mShowAnswerButton = (Button) findViewById(R.id.show_answer_button); //Π΄Π°Π»Π΅Π΅ ΠΊΠΎΠ³Π΄Π° Π½Π°ΠΆΠΈΠΌΠ°Π΅ΠΌ Π½Π° "Show Answer" - ΠΏΠΎΠΊΠ°Π·Π°ΡΡ ΡΠ΅ΠΊΡΡ ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎΠ³ΠΎ ΠΎΡΠ²Π΅ΡΠ° mShowAnswerButton.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); } }); } private void setAnswerShownResult (boolean isAnswerShown) { Intent data = new Intent(); data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown); setResult(RESULT_OK, data); } @Override public void onSaveInstanceState(Bundle saveInstanceState) { super.onSaveInstanceState(saveInstanceState); Log.d(TAG, "onSaveInstanceState"); saveInstanceState.putBoolean(EXTRA_ANSWER_SHOWN,__________); }