Which Android Fragment lifecycle methods require super

荒凉一梦 提交于 2019-11-30 19:24:11

All of the corresponding Activity lifecycle methods except onSaveInstanceState require calls to super. In addition:

  • onAttach() - yes
  • onActivityCreated() - yes
  • onViewStateRestored() - is not a Fragment method
  • onDestroyView() - yes
  • onDetach() - yes
  • onSaveInstanceState() - from Fragment#onSaveInstanceState it looks like a no

All of the methods that require calls to super share the first line of their method in android.app.Fragment: mCalled = true;

That way the FragmentManager can check if mCalled is true and throw a SuperNotCalledException when it is not called. See FragmentManager#moveToState to see this implementation.

When generating a fragment with Eclipse, the onCreateView method template code does not have a call to super.onCreateView. Also, the generally quite good book published by WROX: Android 4 Application Development misses it out in its sample lifetime code (it does not miss out any other calls to super).

Of course both these two sources could be incorrect, but using the Eclipse template and not adding super.onCreateView has not caused me an issue.

I am typing with capital letter 'O' instead of small letter 'o'

means : OnCreate instead of onCreate methods.

Its a silly mistake but need to remember :)

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!