4

I'm trying to get a list of all Android lifecycle methods that require calling the superclass method. There was a good list started in another thread. I'm not asking because of an error in my code, but because I want a tool to detect this specific problem.

To find the list, I searched the API 18 docs for the following strings:

  • Derived classes must call through to the super class's
  • if you override this method
  • superclass
  • SuperNotCalledException from platform sources

I ended up with the following list. I removed derived classes that have the same message as the parent.

  • Application (MockApplicaition)
    • onCreate()
  • Activity/ActivityGroup (Fragment/DialogFragment, ListActivity, TabActivity, LauncherActivity, NativeActivity, et. al)
    • onCreate(android.os.Bundle)
    • onDestroy()
    • onPause()
    • onPostCreate(android.os.Bundle)
    • onPostResume()
    • onRestart()
    • onResume()
    • onStart()
    • onStop()
    • onConfigurationChanged(Configuration newConfig)
    • onSaveInstanceState(android.os.Bundle) - Depends
    • onRestoreInstanceState(android.os.Bundle) - Depends
    • onActionModeStarted(android.view.ActionMode) - Recommended
    • onActionModeFinished(android.view.ActionMode) - Recommended
    • onPrepareDialog(int, android.app.Dialog, android.os.Bundle) - Recommended
  • Fragment
    • onAttach(FragmentActivity)
    • onDetach()
    • onViewStateRestored(android.os.Bundle)
    • onActivityCreated(Bundle)
    • onDestroyView()
  • Dialog (AlertDialog, DatePickerDialog, ProgressDialog, Presentation, et. al)
    • onActionModeStarted(android.view.ActionMode) - Recommended
    • onActionModeFinished(android.view.ActionMode) - Recommended
  • ViewPager
    • onPageScrolled(int, float, int)
    • drawableStateChanged()
  • View/ViewGroup (ListView, TextView, KeyboardView, et. al.)
    • onAnimationStart()
    • onAnimationEnd()
    • onMeasure(int, int) - Required to call setMeasuredDimension(int, int)
    • draw(android.graphics.Canvas)
  • Preference/PreferenceGroup (CheckBoxPreference, et. al.)
    • onBindView(android.view.View)
    • onCreateView(android.view.ViewGroup)
    • onPrepareForRemoval()
    • onCommonsWare()

Is this the definitive list?

EDIT Based on feedback from @CommonsWare, added methods that trigger SuperNotCalledException

Community
  • 1
  • 1
Eric Cloninger
  • 2,260
  • 2
  • 21
  • 26
  • 2
    I would instead search for occurrences of `SuperNotCalledException` in the source -- if that's not thrown, you should be able to avoid the `super` call. Also note that `onCommonsWare()` is deprecated prior to API Level 42. – CommonsWare Sep 19 '13 at 19:53
  • @CommonsWare Good point. As soon as my build box stops emitting smoke from building a certain OEM Android distro, I'll grep. Then I'll update this list. – Eric Cloninger Sep 19 '13 at 20:02
  • Updated list using SuperNotCalledException. Thanks @commonsware! – Eric Cloninger Sep 20 '13 at 00:12
  • Can anyone weigh in on why this seems to be so poorly documented? Pretty sure I remember reading something about OOP and hiding implementation details, so I don't think that reading the class source and seeing if it might break for unknown reasons if `super` isn't called is a sound programming technique. And what about methods like `Fragment#onCreateView` where it can go either way? Bizzare. – jordanpg Jun 17 '14 at 23:25

0 Answers0