9

Currently (Android API 17), the only mention of super in the Android Reference on Fragment is casually via some code examples (unlike the Android Reference on Activity, which carefully notes where super is required).

SO suggests searching the web as needed, or waiting for a crash, to identify where a call to super is required. I'm asking SO users to share their knowledge on which of the Fragment lifecycle methods require a call to super.

Fragment lifecycle methods - require call to super

  • onAttach()
  • onCreate() - presumably yes, as Activity version requires it
  • onCreateView() - seems ok with or without
  • onActivityCreated()
  • onViewStateRestored()
  • onStart() - presumably yes, as Activity version requires it
  • onResume() - presumably yes, as Activity version requires it

  • onPause() - presumably yes, as Activity version requires it

  • onStop() - presumably yes, as Activity version requires it
  • onDestroyView()
  • onDestroy() - presumably yes, as Activity version requires it
  • onDetach()

  • onSaveInstanceState() - presumably yes, as Activity version requires it

Community
  • 1
  • 1
donfede
  • 734
  • 1
  • 9
  • 25
  • 1
    i know this does not answer your question, but it won't hurt to call super always. – Jeffrey Blattman Jan 26 '13 at 00:08
  • 3
    @JeffreyBlattman That's a dangerous statement. I can think of several class methods where calling super or not changes the semantics. onDraw() for example. I don't know the answer to the OP but it's a valid, and important question. – Simon Jan 26 '13 at 00:11
  • I am with @Jeffrey Blattman, and would go with the assumption that calling `super` is always required, except in specific cases. Thus, i would flip the question and phrase it as `"For which methods calling super can have side effects and should not be done"` – Franci Penov Jan 26 '13 at 00:15

3 Answers3

5

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.

user697495
  • 590
  • 1
  • 3
  • 9
  • many thx; got the android source from https://android.googlesource.com/platform/manifest , and can now see the corresponding fragment code and `mCalled` statements. SO Q with some additional info - http://stackoverflow.com/questions/10843383/why-do-we-have-to-call-super-in-android-sometimes#comment14121604_10843452 – donfede Jan 29 '13 at 02:57
0

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.

Steve Waring
  • 2,882
  • 2
  • 32
  • 37
0

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

sssvrock
  • 549
  • 7
  • 8