For View
s having IDs, they will be auto-saved when calling super.onSaveInstanceState(outState);
.
For Fragment
s added to an Activity
, what are the cases that it will be re-created when its Activity
is re-created (e.g. screen rotation), and what are the cases it will not? What to determine? What is the rule?
So far, i have tried the following cases. But trial-n-error does not mean any rules or solutions.
Cases when Fragment
s are re-stored:
- Normal case:
FragmentTransaction.add()
to the layout with an ID. - Fragment without a UI:
FragmentTransaction.add()
to a tag only
Cases when Fragment
s are NOT re-stored:
- When
super.onSaveInstanceState(outState);
is skipped. - Restored
Fragment
s in a UI without a matching ID.
What is the general rule? Anything i missed in the documentation?
Thanks in advance.
Edit:
To my understanding, and experiments, ALL dynamically (programmatically) added Fragment
s are saved upon their Activity
calling super.onSaveInstanceState(outState)
.
- ALL dynamically (programmatically) added
Fragment
s includes,Fragment
s with only a tag (no UI),Fragment
s attached to aView
(with UI),- and
Fragment
s with both an UI and a tag. - (is there any types else?)
Regarding restoring a Fragment
with an UI into a layout that with no matching ID, the Fragment
is indeed re-created. It just cannot be shown visually in the layout, with the following warning message:
04-08 11:41:22.445: W/PhoneWindow(9853): Previously focused view reported id 2131165226 during save, but can't be found during restore.
Once we are back into an UI with its matching ID, it will be restored correctly.