Bug in the official Android Fragments training sample?
- by Jeff Axelrod
It seems to me that there must be a bug in the Android Fragments demo.
As background, Fragments are apparently sometimes instantiated by the Android OS and thus need a public no-arg constructor:
All subclasses of Fragment must include a public empty constructor.
The framework will often re-instantiate a fragment class when needed,
in particular during state restore, and needs to be able to find this
constructor to instantiate it. If the empty constructor is not
available, a runtime exception will occur in some cases during state
restore.
But the NewsReader demo from the official Android training on Fragments constructs the HeadlinesFragment class and configures it with setOnHeadlineSelectedListener(this) from NewsReaderActivity.onCreate().
If the Android OS re-instantiates this fragment, the mHeadlineSelectedListener field will be null because HeadlinesFragment doesn't save or restore its state.
Is this a bug or am I missing something?