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,__________); } 

    2 answers 2

    1) First you need to declare the variable private boolean mWasAnswerShown

    2) In the onCreate method, you need to retrieve the saved value in this way:

     if (savedInstanceState != null) { mWasAnswerShown = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false); // Π²Ρ‚ΠΎΡ€ΠΎΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ - Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ // Ссли Π² savedInstanceState Π½Π΅ окаТСтся ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° с Ρ‚Π΅Π³ΠΎΠΌ EXTRA_ANSWER_SHOWN } 

    3) And in the onSaveInstanceState method, save the value of the mWasAnswerShown variable in this way:

     saveInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mWasAnswerShown); 

    PS If you run CheatActivity activations from those activations with questions and you need some CheatActivity results, you can also use CheatActivity in your activations with pending results:

     Intent startCheatActivityIntent = new Intent(this, CheatActivity.class); startActivityForResult(startCheatActivityIntent, 1); 

    In the CheatActivity code, put the required data into the intent:

     Intent resultsIntent = new Intent(); resultsIntent.putExtra(EXTRA_ANSWER_SHOWN, mWasAnswerShown); setResult(RESULT_OK, resultsIntent); finish(); 

    When returning from CheatActivity in the activation code with questions, you will have access to this intent, in which you put the data, in the following method:

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data == null) return; boolean wasAnswerShown = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN); if(wasAnswerShown) Toast.makeText(QuestionsActivity.this, "Π§ΠΈΡ‚Π΅Ρ€ΠΈΡ‚ΡŒ Π½Π΅Ρ…ΠΎΡ€ΠΎΡˆΠΎ", Toast.LENGTH_SHORT).show(); } 
    • one
      Super, this is what you need! Thank you) A little more confusing than I thought. - Alexey Efimov
    • I ask you, explain a little, the value of the declared variable mWasAnswerShown be through the key EXTRA_ANSWER_SHOWN - right? In general, what are these keys for? - Alexey Efimov
    • @ AlekseyEfimov, to identify objects. Since, for example, in your case, in Intent you can put two values ​​of the type boolean mAnswerIsTrue and mWasAnswerShown , therefore for each variable you need to have such a tag. Usually they are declared as public static final in any one place - A. Shakhov
    • That is, for the variables mentioned, mAnswerIsTrue and mWasAnswerShown must have different keys, otherwise there will be conflicts, and if I specify the key of the necessary variable in the onSaveInstanceState method, then its value will be saved - right? - Alexey Efimov
    • @ AlekseyEfimov, yes, there is a key for each object, since everything is stored in the Bundle as key-value pairs. Accordingly, under what key you save, under this and should be pulled back - A. Shakhov

    When you rotate the screen, the onCreate method is called again. It seems to me that you can override the onStop or onPause methods and save states in them. And in the onCreate method, see if the saved state is empty and, if not, pull it out.