0

Is this correct?

  1. In layout have a checkbox marked as invisible or gone.
  2. After creation, mark the checkbox as visible (i.e. after selecting a button).
  3. Perform a configuration change (rotate device for example).
  4. Checkbox previously marked as visible is not showing.

However, the checked state of the checkbox IS maintained, which is confusing to me, what information is saved, what is not?

What's the best way to ensure the view stays visible after the configuration change?

Thanks, Zach

Zach
  • 3,909
  • 6
  • 25
  • 50

2 Answers2

1

Unless you specify otherwise, a configuration change will cause your current activity to be destroyed. Try to check the visibilty of that checkbox is visiable or not.

In another case, rotate device will cause the layout change, check your layout file with both landscape and portrait, check if you can see the checkbox placed properly.

Sampson
  • 11
  • 2
1

Configuration change forces the activity go through a full life cycle. This means it will be destroyed together with your View and recreated.

Visibility state is not kept by default for Views. Definition for onSaveInstanceState() is here.

"This state should only contain information that is not persistent or can not be reconstructed later."

You can either use a custom CheckBox implementation that saves its visibility state, or save this state as a boolean (buttonPressed = true) in your activity/fragment and set visibility after you inflate your layout. The latter would be easier.

Oguz Babaoglu
  • 299
  • 2
  • 7
  • Thank you for the reply. In my current example I am using an Activity. if I were to save a 'buttonPressed = true' state, I would have to "put" that somewhere as well. In the bundle, presumably? Is that correct? – Zach Apr 14 '15 at 21:07
  • @Zach yes, you can save this boolean in the 'outState' bundle in onSaveInstanceState(Bundle outState). And get it back from the 'savedInstanceState' bundle in onCreate(Bundle savedInstanceState) – Oguz Babaoglu Apr 14 '15 at 21:12