Which Android Fragment lifecycle methods require super

后端 未结 3 1702
一向
一向 2021-01-12 02:21

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 A

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-12 02:49

    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.

提交回复
热议问题