Let the framework manage fragments
The original crash is deep in the framework. Looking into the code, I
found the following strangeness, which this change fixes:
1. Fragments don't have a public no-arg constructor, which is required
for the framework to restore state.
2. Fragments keep a reference to the adapter, passed through the
constructor. This is a terrible idea: the adapter might get stale,
it might not be tied to the current activity, it can't be parcelled
into a bundle, .... At the very least, the adapter should come
directly via the activity.
3. A lot of computation is happening in onAttach(Activity) which is
too early. At this point, the activity is made, but has not gone
through onCreate() yet, so might be uninitialized. Most of this
code should move to onCreateView() or, even better,
onActivityCreated()
4. The index of the current fragment might still be negative in
onCreate(). This causes problems with the ViewPager when it tries
to load the current fragment from the adapter. We should never set
the current position to -1.
5. The computation for currentPosition is incorrect in
PhotoViewActivity.onCreate(), from a cursory glance at
MessageAttachmentTile.viewAttachment() where both the photo index
and the photo uri are populated at the same time. Once the index
is read, it will be > 0, and the URI won't be set. This is a bug.
These changes ameliorate the original issue, but don't fix it
altogether. The remaining issues will be fixed in a subsequent CL.
Bug: 8143282 PhotoViewerActivity crashes on orientation change
Change-Id: Ia0652b2eea9e976603f0d6686077b9f6a75abb78
4 files changed