目前(Android的API 17),只有提起的超
在Android的借鉴片段通过一些code例子是随便(不像在Android的借鉴活动,该仔细注意到其中超
是必需的)。
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建议searching根据需要,或等待崩溃的网页,以确定哪些调用超
是必需的。我问,这样用户分享他们对其中的片段的生命周期方法需要调用知识超
。
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
.
onAttach()
的onCreate()
- presumably是的,因为活动
版本需要它
onCreateView()
- 带或不带似乎确定
onActivityCreated()
onViewStateRestored()
ONSTART()
- presumably是的,因为活动
版本需要它
onResume()
- presumably是的,因为活动
版本需要它。
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是的,因为活动
版本需要它。
onPause()
- presumably yes, as Activity
version requires it
onDetach()
的onSaveInstanceState()
- presumably是的,因为活动
版本需要它。
onSaveInstanceState()
- presumably yes, as Activity
version requires it
所有的相应的活动的生命周期方法,除了的onSaveInstanceState
需要调用超。另外:
All of the corresponding Activity lifecycle methods except onSaveInstanceState
require calls to super. In addition:
onAttach()
- 对
onActivityCreated()
- 对
onViewStateRestored()
- 是不是一个片段方法
onDestroyView()
- 对
onDetach()
- 对
的onSaveInstanceState()
- 从片段#的onSaveInstanceState 该
看起来像一个没有
onAttach()
- yes
onActivityCreated()
- yes
onViewStateRestored()
- is not a Fragment method
onDestroyView()
- yes
onDetach()
- yes
onSaveInstanceState()
- from Fragment#onSaveInstanceState it
looks like a no
所有的需要调用超级份额及其在android.app.Fragment方法的第一线的方法:
mCalled = TRUE;
All of the methods that require calls to super share the first line of their method in android.app.Fragment:
mCalled = true;
这样的FragmentManager可以检查是否mCalled是真实的,并抛出一个SuperNotCalledException时,那不叫。请参阅 FragmentManager#moveToState 看到这个实现。
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.