Help guys figure it out. My first test program.

There are two activites: MainActivity and MainActivityTwo. I turn between them like this:

Intent gotoanotheractivity = new Intent(MainActivity.this, MainActivityTwo.class); startActivity(gotoanotheractivity); 

And from the second to the first:

 Intent gotoanotheractivity = new Intent(MainActivityTwo.this, MainActivity.class); startActivity(gotoanotheractivity); 

In MainActivityTwo I give the user a bonus - an asterisk for 12 correct questions. I keep it with

SharedPreferences and in the MainActivity (application menu) get out of the settings I bring it.

MainActivity:

 if(mySettings1.contains(Tobe_Bonus_Star1)) { // получение 1 бонусной звездочки image_tobe1.setImageResource(R.drawable.star_gold); image_tobe1.setVisibility(View.VISIBLE); } 

Everything works, but when I finish the second activation with the finish () method, the bonuses are not displayed.

Tell me why?

  • Probably you output the asterisk to onCreate, but it is not called when you return to the previous activation. Try setting the asterisk in onResume to transfer - YuriySPb
  • Thank you, YurySPb. Everything is working. - Anna_True_sheva
  • I wrote back in more detail. You can mark it as correct (tick to the left of the response body) and vote for it (arrows are also to the left of the response body) - Yuriy SPb

1 answer 1

The thing is, what lifecycle methods will be invoked with different ways of displaying activations. When you launch an activit through an intent, in general, an activit will be created and its onCreate() method will be called, in which you will show the asterisk.

In the case when you return from the second activation to the first through the completion of the second method finish() , you do not create the first activation, the onCreate() method is not called and you do not see the changes on the screen.

In your case, you can act in different ways. The easiest way: set everything you need in onResume() . The second, more correct one is to subscribe to changes in the sharedPreferences in the sharedPreferences and in its callback to perform the necessary actions.