0

This is really a two questions pertaining to lifecycle.

1) In Fragment.onCreateView(LayoutInflater, ViewGroup container, Bundle), all of the examples I've seen simply use the LayoutInflater to inflate a View, and return it. If this is part of a Restore though, i.e. non-null Bundle, shouldn't the restoration of the view hierarchy be handled by the system? Should I instead be calling container.findViewById() or trying to pull the view out of the Bundle or something (for the purposes of pulling out references to subviews)?

2) In general, do any of the Fragment lifecycle callbacks need to worry about saving/restoring the state of its view hierarchy, even implicitly by calling super.onXXX()? Or is that all handled uplevel by the owning Activity when it calls super.onCreate(Bundle)?

Kevin Lam
  • 445
  • 3
  • 9

1 Answers1

1
  1. Although the framework is responsible for re-creating the Fragment itself, the view hierarchy must be recreated manually. Views cannot survive the destruction of their Activity (plus, since onCreateView() has your implementation, you could conditionally inflate another layout or do different things -- that's why it has to run every time). The Bundle contains information put there by onSaveInstanceState(), but the old views are not part of it.

  2. If the view ids match between the old and new layout, then the state should be automatically restored (via the super calls). Views override their own onSaveInstanceState() for this. If you save your custom state in the fragment's onSaveInstanceState(), then you're also responsible for restoring it.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Ah, this makes more sense to me. I also got mixed up a bit by the answer to this: http://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state - so sounds to me like while Fragments may need to be stored and re-looked-up, Views are always new? – Kevin Lam Jun 16 '14 at 23:56
  • Yes. In short, whenever an activity is destroyed, its views are too. – matiash Jun 17 '14 at 01:26