<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文:[http://developer.android.com/training/basics/](http://developer.android.com/training/basics/activity-lifecycle/pausing.html)[activity](# "An activity represents a single screen with a user interface.")-lifecycle/pausing.html 在正常使用app時,前端的[activity](# "An activity represents a single screen with a user interface.")有時會被其他可見的組件阻塞(obstructed),從而導致當前的[activity](# "An activity represents a single screen with a user interface.")進入Pause狀態。例如,當打開一個半透明的[activity](# "An activity represents a single screen with a user interface.")時(例如以對話框的形式),之前的[activity](# "An activity represents a single screen with a user interface.")會被暫停。 只要之前的[activity](# "An activity represents a single screen with a user interface.")仍然被部分可見,這個[activity](# "An activity represents a single screen with a user interface.")就會一直處于Paused狀態。 然而,一旦之前的[activity](# "An activity represents a single screen with a user interface.")被完全阻塞并不可見時,則其會進入Stop狀態(將在下一小節討論)。 [activity](# "An activity represents a single screen with a user interface.")一旦進入paused狀態,系統就會調用[activity](# "An activity represents a single screen with a user interface.")中的[onPause()](http://developer.android.com/reference/android/app/Activity.html#onPause())方法, 該方法中可以停止不應該在暫停過程中執行的操作,如暫停視頻播放;或者保存那些有可能需要長期保存的信息。如果用戶從暫停狀態回到當前[activity](# "An activity represents a single screen with a user interface."),系統應該恢復那些數據并執行[onResume()](http://developer.android.com/reference/android/app/Activity.html#onResume())方法。 > **Note:** 當我們的[activity](# "An activity represents a single screen with a user interface.")收到調用onPause()的信號時,那可能意味者[activity](# "An activity represents a single screen with a user interface.")將被暫停一段時間,并且用戶很可能回到我們的[activity](# "An activity represents a single screen with a user interface.")。然而,那也是用戶要離開我們的activtiy的第一個信號。 ![basic-lifecycle-paused](https://box.kancloud.cn/2015-07-28_55b7246fcd4f9.png) **Figure 1.** 當一個半透明的[activity](# "An activity represents a single screen with a user interface.")阻塞[activity](# "An activity represents a single screen with a user interface.")時,系統會調用onPause()方法并且這個[activity](# "An activity represents a single screen with a user interface.")會停留在Paused 狀態(1). 如果用戶在這個[activity](# "An activity represents a single screen with a user interface.")還是在Paused 狀態時回到這個[activity](# "An activity represents a single screen with a user interface."),系統則會調用它的onResume() (2). ### 暫停[Activity](# "An activity represents a single screen with a user interface.") 當系統調用[activity](# "An activity represents a single screen with a user interface.")中的onPause(),從技術上講,意味著[activity](# "An activity represents a single screen with a user interface.")仍然處于部分可見的狀態.但更多時候意味著用戶正在離開這個[activity](# "An activity represents a single screen with a user interface."),并馬上會進入Stopped state. 通常應該在onPause()回調方法里面做以下事情: - 停止動畫或者是其他正在運行的操作,那些都會導致CPU的浪費. - 提交在用戶離開時期待保存的內容(例如郵件草稿). - 釋放系統資源,例如broadcast receivers, sensors (比如GPS), 或者是其他任何會影響到電量的資源。 例如, 如果程序使用[Camera](http://developer.android.com/reference/android/hardware/Camera.html),onPause()會是一個比較好的地方去做那些釋放資源的操作。 ~~~ @Override public void onPause() { super.onPause(); // Always call the superclass method first // Release the Camera because we don't need it when paused // and other activities might need to use it. if (mCamera != null) { mCamera.release() mCamera = null; } } ~~~ 通常,**不應該**使用onPause()來保存用戶改變的數據 (例如填入表格中的個人信息) 到永久存儲(File或者DB)上。僅僅當確認用戶期待那些改變能夠被自動保存的時候(例如正在撰寫郵件草稿),才把那些數據存到永久存儲 。但是,我們應該避免在onPause()時執行CPU-intensive 的工作,例如寫數據到DB,因為它會導致切換到下一個[activity](# "An activity represents a single screen with a user interface.")變得緩慢(應該把那些heavy-load的工作放到[onStop()](http://developer.android.com/reference/android/app/Activity.html#onStop())去做)。 如果[activity](# "An activity represents a single screen with a user interface.")實際上是要被Stop,那么我們應該為了切換的順暢而減少在OnPause()方法里面的工作量。 > **Note:**當[activity](# "An activity represents a single screen with a user interface.")處于暫停狀態,[Activity](# "An activity represents a single screen with a user interface.")實例是駐留在內存中的,并且在[activity](# "An activity represents a single screen with a user interface.") 恢復的時候重新調用。我們不需要在恢復到Resumed狀態的一系列回調方法中重新初始化組件。 ### 恢復[activity](# "An activity represents a single screen with a user interface.") 當用戶從Paused狀態恢復[activity](# "An activity represents a single screen with a user interface.")時,系統會調用onResume()方法。 請注意,系統每次調用這個方法時,[activity](# "An activity represents a single screen with a user interface.")都處于前臺,包括第一次創建的時候。所以,應該實現onResume()來初始化那些在onPause方法里面釋放掉的組件,并執行那些[activity](# "An activity represents a single screen with a user interface.")每次進入Resumed state都需要的初始化動作 (例如開始動畫與初始化那些只有在獲取用戶焦點時才需要的組件) 下面的onResume()的例子是與上面的onPause()例子相對應的。 ~~~ @Override public void onResume() { super.onResume(); // Always call the superclass method first // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera init } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看