Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Android Fragment lifecycle methods require super

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

like image 344
donfede Avatar asked Jan 25 '13 23:01

donfede


People also ask

What is difference between onCreate and onCreateView in fragment?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.

What is difference between onViewCreated and onCreateView?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .


1 Answers

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.

like image 130
user697495 Avatar answered Oct 07 '22 07:10

user697495