0

i use this script to change the color button and this works..

private void Btn1_Click(object sender, EventArgs e) { button4.setbackgroundcolor (color.red); }

But Why background color button changed to default again after screen off/lock screen???.. I run this in lollipop 5.0 api 21..

  • because your activity is going to be recreated after the screen turns on again. See about Activity lifecycles here:https://developer.android.com/guide/components/activities/activity-lifecycle.html – Opiatefuchs Apr 25 '17 at 07:03
  • But when i run in android lollipop 5.1 fire 22 it's all no problem .. Sorry i am very beginner .. so what should i do ?? – cinemaputar.com Apr 25 '17 at 07:41
  • This is maybe caused by some energy settings. Not every device kills your activity directly if the screen goes off. Also, be aware of doze mode since Android 6.... – Opiatefuchs Apr 25 '17 at 07:45

1 Answers1

1

When you close the app or Android requires more memory, onDestroy() is called and your app is removed from the memory.

  1. If you want to keep the color permanently, then set button's color in XML or during onCreate use findViewById to find the button and set it's color.
  2. If you want button color to be changed once on button click and then stay like that always, then you should save this information in SharedPreferences. Next time when you app is launched, check if SharedPreferences contains intended button color and if so, apply it.

SharedPreferences are used to store small amount of data in key-value pairs. They are saved in internal storage, so you will get the value even after phone reboots.

See examples for SharedPreferences below:

Saving Key-Value Sets

Android Shared preferences example

Community
  • 1
  • 1
Yogesh
  • 1,230
  • 3
  • 13
  • 18
  • Yes .. i want as you said in the 2nd point .. but i do not understand what is meant by SharedPreferences .. please give more detail example .. Sorry i am very beginner.. so what should i do?? But when i run in android lollipop 5.1 api 22 it's all no problem.. – cinemaputar.com Apr 25 '17 at 07:49