2

When the activity gets restarted all the fragments that where hidden show again. By setting this atribute to true the fragment retains its instance. This looks weird to me and I don't understand why this behavior is not set by default. Is this the best way of doing this?

Héctor Júdez Sapena
  • 5,329
  • 3
  • 23
  • 31

1 Answers1

0

Not sure if there is a single answer to your question of "what is the best way of doing this" but I have commentated on similar 'how does this work' questions. This discussion may assist you in deciding whether to apply setRetainInstance(true) or setRetainInstance(false). Please note that there have been compatibility issues with this method when using the v4.support* libraries. I agree in that retaining the instance isn't the best way forward for a lot of scenarios.

Community
  • 1
  • 1
BrantApps
  • 6,362
  • 2
  • 27
  • 60
  • I have an activity that show a form that changes its layout dynamically depending on the user selections. Every time the screen rotates all the hidden views in the form layout show again.. how am I supposed to handle this? Do I have to store the state of the form in the savedInstanceState bundle and restore the layout of the form to its state everytime there's a config change if I don't use setRetainInstance(true)? – Héctor Júdez Sapena Jul 24 '12 at 11:34
  • Hi Hector, save in ````onSavedInstanceState(...)```` the flags or other signals that your activity uses to display or hide the fields. The ````os.Bundle```` that you save as part of onSavedInstanceState is delivered back to the activity in ````onCreate(Bundle bundle)````. Perform the logic of showing and hiding fields using this information as part of the onCreate or onCreateView (if you are using ````Fragment````s. – BrantApps Jul 24 '12 at 13:42
  • how can we retain configuration changes in fragments which are maintain in back stack. As we cannot use `setRetainInstance(true)` – Dory Apr 04 '14 at 13:21