ActivityLifecycleCallbacks 對Activity的生命週期事件進行集中處理

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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章