Saving UI on orientation change - onSaveInstanceState not working as expected if retaining Fragment
If you use setRetainInstance(true)
then of course the bundle is null. The fragment is not destroyed but only detached from the current activity and attached to the new activity. Only when the fragment is destroyed do you get a bundle with the values you saved in onSaveInstanceState
. Just remove setRetainInstance(true)
and use the saved values in onCreateView() to setup your custom views.
Related videos on Youtube
PJL
Updated on June 04, 2022Comments
-
PJL almost 2 years
Using compat lib v1 (not using v2|3 because of certain bugs); a variation of this question.
I have a fragment whose UI has various controls whose state I want to maintain on an orientation change.
The parent activity is being destroyed on orientation change (PLEASE don't tell me about manifest changes to avoid activity recreation!!!!).
The fragment calls
setRetainInstance(true)
.1) Now my understanding is that Views with unique IDs should retain some state on say an orientation change. Given this I would expect a non-null bundle into
onCreateView|onActivityCreated
but it is null.2) In any case if I save state in
onSaveInstanceState
(ensuring I call super) I still get a null bundle in 'onCreateView|onActivityCreated`3) If I don't call
setRetainInstance(true)
then I DO get a non-null bundle inonCreateView|onActivityCreated
even if I don't have an `onSaveInstanceState' method.The questions I have is, is this working as expected and my understanding of the life-cycle is broken? Regardless, I'm guessing that the best way forward for me would be to retain the fragment and then maintain the state of the controls myself within the fragment.
Thanks in advance. Peter.
-
Catalin Morosan over 12 yearsIf you use setRetainInstance(true) then of course the bundle is null. The fragment is not destroyed but only detached from the current activity and attached to the new activity. Only when the fragment is destroyed do you get a bundle with the values you saved in onSaveInstanceState. Just remove setRetainInstance(true).
-
-
PJL over 12 yearsThen I'm suprised that I get a call to
onSaveInstanceState
on a rotation whensetRetainInstance(true)
is called and furthermore the state of controls are lost. It was my understanding that Views with unique IDs should retain some state on say an orientation change. -
Catalin Morosan over 12 yearsonCreateView() is called no matter if you use setRetainInstance or not. So the entire view will be recreated. Which controls do you actually see that they lose state? setRetainInstance(true) should be used only in very specific situations. What exactly do you want to accomplish? setRetain.. doesn't keep the state of your controls but keeps that fragment alive so you can save various information in your member variables. Then, when onCreateView is called again, you can set the state of your various views with the info from your member variables.
-
PJL over 12 yearsThanks, I realise that
onCreateView
is called regardless and I am managing the state of controls myself. I also appreciate that things are basically working as expected, however, I'm still suprised thatonSaveInstanceState
is called on a rotation which to me gives the false impression thatonCreateView
will then be called with a non-null bundle.