Bug in the official Android Fragments training sample?
Posted
by
Jeff Axelrod
on Stack Overflow
See other posts from Stack Overflow
or by Jeff Axelrod
Published on 2012-09-23T03:34:37Z
Indexed on
2012/09/23
3:37 UTC
Read the original article
Hit count: 372
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?
© Stack Overflow or respective owner