I just read Using context in a fragment cause I need to get a context in order to initialize a database. The 2 most voted answers where using Fragment#getActivity() to get the Context or doing it inside Fragment#onAttach(Activity) callback method.
The thing is, in order to maintain a well documented code, I want to avoid doing things the "alternate" way, but doing it the way it was supposed to.
Judging by Fragment life-cycle found in here: http://developer.android.com/guide/components/fragments.html#Creating seems like onAttach() is called before onCreate, which means getActivity() should NOT return null at this point.
Questions would be:
- is it meant to initialize a database inside
onAttach()? - is it possible for
getActivity()to returnnullwhen called insideonCreate()even thoughonAttach()has already been called?