ActivityLifecycleCallbacks 对Activity的生命周期事件进行集中处理

来源:互联网 发布:js难学吗 编辑:程序博客网 时间:2024/05/16 18:57

ActivityLifecycleCallbacks是什么?

  • Application通过此接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理。

为什么用ActivityLifecycleCallbacks?

  • 以往若需监测Activity的生命周期事件代码,你可能是这样做的,重写每一个Acivity的onResume(),然后作统计和处理:
    <div class="line"><span class="text plain"><span class="meta paragraph text"></span></span></div><div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 898.906px; overflow: hidden; padding-top: 1px; border-color: rgb(204, 204, 204); margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="ViewSource" title="view plain" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="CopyToClipboard" title="copy" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="PrintSource" title="print" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="About" title="?" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/491145" target="_blank" title="在CODE上查看代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/491145/fork" target="_blank" title="派生到我的代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 829px; top: 845px; width: 27px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">protected</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onResume() {  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onResume();  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(170, 202, 179); background-color: inherit;">//TODO 处理和统计代码</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  Log.v(TAG, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onResume"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  Logger.v(TAG, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onResume"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  Logging.v(TAG, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onResume"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  ...  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div><div class="line"><span class="text plain"><span class="meta paragraph text"></span></span></div>
  • ActivityLifecycleCallbacks接口回调可以简化这一繁琐过程,在一个类中作统一处理

ActivityLifecycleCallbacks怎么用?

  • android.app.Application.ActivityLifecycleCallbacks
  • 要求API 14+ (Android 4.0+)
  • 继承Application
    <div class="line"><span class="text plain"><span class="meta paragraph text"></span></span><div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 898.906px; overflow: hidden; padding-top: 1px; border-color: rgb(204, 204, 204); margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="ViewSource" title="view plain" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="CopyToClipboard" title="copy" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="PrintSource" title="print" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="About" title="?" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/491145" target="_blank" title="在CODE上查看代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/491145/fork" target="_blank" title="派生到我的代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 829px; top: 1328px; width: 27px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> BaseApplication </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Application  </span></span></li></ol></div></div>
  • 在AndroidManifest里起用自定义Application
    <div class="line"><span class="text plain"><span class="meta paragraph text"></span></span><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 898.906px; overflow: hidden; padding-top: 1px; border-color: rgb(204, 204, 204); margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[html]</strong> <a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="ViewSource" title="view plain" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="CopyToClipboard" title="copy" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="PrintSource" title="print" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="About" title="?" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/491145" target="_blank" title="在CODE上查看代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/491145/fork" target="_blank" title="派生到我的代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 831px; top: 1497px; width: 27px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-xml" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">application</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">android:name</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">".global.BaseApplication"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li></ol></div></div>
  • 重写Application的onCreate()方法,或在Application的无参构造方法内,调用Application.registerActivityLifecycleCallbacks()方法,并实现ActivityLifecycleCallbacks接口

    <div class="line"><span class="text plain"><span class="meta paragraph text"></span></span></div><div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 898.906px; overflow: hidden; padding-top: 1px; border-color: rgb(204, 204, 204); margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="ViewSource" title="view plain" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="CopyToClipboard" title="copy" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="PrintSource" title="print" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="About" title="?" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/491145" target="_blank" title="在CODE上查看代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/491145/fork" target="_blank" title="派生到我的代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 829px; top: 1701px; width: 27px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onCreate() {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onCreate();  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.registerActivityLifecycleCallbacks(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ActivityLifecycleCallbacks() {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivityStopped(Activity activity) {  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivityStopped"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivityStarted(Activity activity) {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivityStarted"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivitySaveInstanceState(Activity activity, Bundle outState) {  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivitySaveInstanceState"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivityResumed(Activity activity) {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivityResumed"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivityPaused(Activity activity) {  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivityPaused"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivityDestroyed(Activity activity) {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivityDestroyed"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onActivityCreated(Activity activity, Bundle savedInstanceState) {  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        Logger.v(activity, <span class="string" style="margin: 0px; padding: 0px; border: none; color: rgb(255, 153, 0); background-color: inherit;">"onActivityCreated"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    }  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  });  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">};  </span></li></ol></div><div class="line"><span class="text plain"><span class="meta paragraph text"></span></span></div>
  • 运行结果(Logcat日志)

    <div class="line"><span class="text plain"><span class="meta paragraph text"></span></span></div><div class="dp-highlighter bg_plain" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 898.906px; overflow: hidden; padding-top: 1px; border-color: rgb(204, 204, 204); margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[plain]</strong> <a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="ViewSource" title="view plain" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="CopyToClipboard" title="copy" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="PrintSource" title="print" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/tongcpp/article/details/40344871#" class="About" title="?" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/491145" target="_blank" title="在CODE上查看代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/491145/fork" target="_blank" title="派生到我的代码片" style="text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; color: rgb(12, 137, 207); background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 833px; top: 2572px; width: 27px; height: 15px; z-index: 99;"></div></div></div><ol start="1" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">10-21 14:32:57.722: V/WelcomeActivity(8085): onActivityCreated  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:57.762: V/WelcomeActivity(8085): onActivityStarted  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:57.762: V/WelcomeActivity(8085): onActivityResumed  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:59.164: V/WelcomeActivity(8085): onActivityPaused  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:59.194: V/MainActivity(8085): onActivityCreated  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:59.224: V/MainActivity(8085): onActivityStarted  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:59.224: V/MainActivity(8085): onActivityResumed  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:59.735: V/WelcomeActivity(8085): onActivityStopped  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:32:59.735: V/WelcomeActivity(8085): onActivityDestroyed  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:33:06.502: V/MainActivity(8085): onActivityPaused  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:33:06.612: V/MainActivity(8085): onActivityStopped  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(119, 119, 119); line-height: 18px; background-color: rgb(249, 249, 249);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">10-21 14:33:06.612: V/MainActivity(8085): onActivityDestroyed  </span></li></ol></div><div class="line"><span class="text plain"><span class="meta paragraph text"></span></span></div>

ActivityLifecycleCallbacks的拓展用法

  • 本次初探仅尝试使用Log日志工具作简要测试,如需满足较复杂的统计或调试需求时,此法可能会大大减少插入代码量,提高效率

  • API仅在14+版本上提供此接口回调,Android 4.0以下系统如何使用?

  • API仅针对上述几个Activity的生命周期事件留出了接口回调,可能已无法满足日益过渡为使用Fragment的今日需求, 如何在更大范围内应用LifecycleCallbacks?
0 0