0

I've just about got the concept of using the Bundle instance state to store stuff that wouldn't otherwise be stored when an Activity is destroyed and recreated (e.g. on screen rotation). I also see that you don't need to do this for basic information for each View, like text in a TextView, because the system does that for you (at least for those Views that have an ID assigned). See http://developer.android.com/training/basics/activity-lifecycle/recreating.html.

But when I change the background color of a View programmatically (I'm using a basic View as a color swatch linked to a color picker) using setBackgroundColor(), I find that the color is lost when the screen rotates, and reverts back to the original setting. I do have an ID assigned to the View.

Should that color information be preserved automatically, or am I just being hopeful, and do I have to keep track of that separately and restore the color on recreation of the Activity?

Thanks.

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
  • How surprising, I found a page in the official BASIC training page by doing a simple Google Search : http://developer.android.com/training/basics/activity-lifecycle/recreating.html I wonder what prevented you to do the same ... – 2Dee Jan 29 '15 at 13:45
  • Can't you see that I put the very same link in my original post!! So clearly I *did* bother searching and I *did* find that BASIC training page. But I can't see any mention in the BASIC training page of preserving background colors, which is my particular issue. Thanks for being so patronising though. – drmrbrewer Jan 29 '15 at 17:00

1 Answers1

0
  • Yes when you set your Background Color directly by View.setBackgroundColor() Always when the app is changed to landscape this is replace by the default layout. You should implement:

    setContentView(R.layout."activitylayoutname");

  • And of course on the layout set a background parameters.

Edited 1: Try this link to set background valúes:

Setting background colour of Android layout element.

And also if you want to preserve other valúes between Activities, just recreate an activity:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Community
  • 1
  • 1
MarcS
  • 681
  • 5
  • 12
  • Thanks. I fit it a bit odd that Android doesn't preserve the colors on a screen re-orientation. Anyhow, I've put some code into `onSaveInstanceState()` and `onRestoreInstanceState()` to do this myself, and it's working nicely. – drmrbrewer Feb 02 '15 at 07:54