其中Android的碎片生命周期方法需要超 [英] Which Android Fragment lifecycle methods require super

查看:219
本文介绍了其中Android的碎片生命周期方法需要超的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前(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建议<一href="http://stackoverflow.com/questions/10843383/why-do-we-have-to-call-super-in-android-sometimes">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() - 从<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/app/Fragment.java#Fragment.onSaveInstanceState%28android.os.Bundle%29"相对=nofollow>片段#的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时,那不叫。请参阅<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/app/FragmentManager.java#FragmentManagerImpl.moveToState%28android.app.Fragment,int,int,int,boolean%29"相对=nofollow> 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.

这篇关于其中Android的碎片生命周期方法需要超的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆