多Activity切換的生命週期問題

Burjal本尊發表於2017-12-21

我們日常開發中,自認為對Activity的生命週期瞭然於胸(onCreate , onStart , onResume , onPause , onStop , onDestroy , onRestart),但是,是真的對Activity宣告週期融會貫通了嗎? 對透明主題的 Activity 的生命週期變化呢?

本文對 AndroidActivity 間互動所引起的生命週期變化進行詳細的分析~

注: 下文所有 Activity 均指普通風格,非透明的 Activity,透明風格的 Acitivity 統一表示為 TransActivity


Q1: Activity1 啟動 Activity2 問題

** Question:描述 MainActivity 啟動 SecondActivity ,生命週期變化過程?**

** Answer:**

  • MainActivity 啟動: onCreate() --- onStart() --- onResume()
  • startActivity(new Intent(MainActivity.this, SecondActivity.class)) 流程: MainActivity - onPause() --- SecondActivity - onCreate() --- SecondActivity - onStart() --- SecondActivity - onResume() --- MainActivity - onStop

重點(敲黑板):

我們都知道 Activity 停止活動的時候流程是 onPause - onStop,但是在結合啟動一個新的 Activity 的時候呢?

答案就是:

1、當前一個 Activity 執行完 onPause() 方法後,才會執行啟動的 ActivityonCreate() 方法; 2、當啟動的 Activity 執行完 onResume() 方法後才會去執行前一個 ActivityonStop() 方法。

按照基本法,附上測試的Log資訊:

//啟動 MainActivity
04-14 23:34:52.692 22963-22963/com.burjal.performancetest I/MainActivity: ====onCreate=====
04-14 23:34:52.692 22963-22963/com.burjal.performancetest I/MainActivity: ====onStart=====
04-14 23:34:52.692 22963-22963/com.burjal.performancetest I/MainActivity: ====onResume=====

//從 MainActivity 開啟 SecondActivity
04-14 23:34:57.532 22963-22963/com.burjal.performancetest I/MainActivity: ====onPause=====
04-14 23:34:57.562 22963-22963/com.burjal.performancetest I/SecondActivity: ====onCreate=====
04-14 23:34:57.562 22963-22963/com.burjal.performancetest I/SecondActivity: ====onStart=====
04-14 23:34:57.562 22963-22963/com.burjal.performancetest I/SecondActivity: ====onResume=====
04-14 23:34:57.972 22963-22963/com.burjal.performancetest I/MainActivity: =====onStop====
複製程式碼

總結:

1、優化 Activity 啟動速度的時候,把相關資料持久化的耗時操作放在 onStop() 方法中,因為新的Activity 是在前一個 Activity 執行完 onPause() 方法才會開始建立; 2、Activity 介面對使用者可見是在 onResume() 方法執行完之後,所以一味地把相關啟動操作從 onCreate() 移動到 onResume() 方法,成效並不大。

經過上述問題,我們又會想到,那從 SecondActivity 返回到 MainActivity ,兩個 Activity 的生命週期變化流程又是如何的呢?下面我們引入 Q2 問題。


Q2: 從 Activity2 返回 Activity1 問題

** Question:描述 SecondActivity 返回到 MainActivity ,生命週期變化過程?**

** Answer:**

  • onBackPressed() 流程: SecondActivity - onPause() --- MainActivity - onRestart() --- MainActivity - onStart() --- MainActivity - onResume() --- SecondActivity - onStop() --- SecondActivity - onDestroy()

眾所周知,onBackPressed() 後直接呼叫到返回的 ActivityonRestart() 方法,但是我們很少結合到兩個 Activity 互動並綜合去看它們的宣告週期問題。

通過 Q1 問題,其實很容易想到 Q2 的答案,就是在 MainActivityonResume() 方法執行完後再去順序執行 SecondActivityonStop() , onDestroy() 方法。

同樣,附上測試的Log資訊:

04-14 23:48:10.692 22963-22963/com.burjal.performancetest I/SecondActivity: ====onPause=====
04-14 23:48:10.692 22963-22963/com.burjal.performancetest I/MainActivity: ====onRestart=====
04-14 23:48:10.692 22963-22963/com.burjal.performancetest I/MainActivity: ====onStart=====
04-14 23:48:10.692 22963-22963/com.burjal.performancetest I/MainActivity: ====onResume=====
04-14 23:48:11.062 22963-22963/com.burjal.performancetest I/SecondActivity: =====onStop====
04-14 23:48:11.062 22963-22963/com.burjal.performancetest I/SecondActivity: ====onDestroy=====
複製程式碼

通過上述兩個試驗我們得出如下結論(敲黑板,這是真重點啊,盆友們):

兩個 Activity 涉及到互相切換的時候,執行完前一個 ActivityonPause() 方法後,立即進入目標 Activity 對應生命週期中去,當目標 Activity 執行完 onResume() 方法後,再順序執行進入到前一個 ActivityonStop() 方法(返回的情況下還有 onDestroy() 方法)。

所以,在 Activity 啟動速度優化上,也要考慮到 onPause() 減負問題。


Q3: Activity1 啟動 TransActivity2 問題

** Question:描述 LifecycleListActivity 啟動 LifecycleTransActivity ,生命週期變化過程?**

首先,我們需要明確的是,在 Android 中設定透明主題方式可以通過 Theme.Translucent 實現。

** Answer:**

  • startActivity(new Intent(LifecycleListActivity.this, LifecycleTransActivity.class)) 流程: LifecycleListActivity - onPause() --- LifecycleTransActivity - onCreate() --- LifecycleTransActivity - onStart() --- LifecycleTransActivity - onResume()

** 重點(敲黑板):**

  • 對比與 Q1 開啟普通 Activity 對比,少了前一個 ActivityonStop() 方法
  • 我們都知道 onPause() 方法是當前 Activity 即將進入後臺時呼叫,onStop() 方法是當前 Activity 不在對使用者可見時呼叫
  • LifecycleListActivity 開啟 LifecycleTransActivity 後,由於 LifecycleListActivity 進入後臺,所以此時會呼叫 LifecycleListActivity - onPause() 方法。在 LifecycleTransActivity - onResume() 後,此時由於 LifecycleTransActivity 是透明主題,所以 LifecycleListActivity 對使用者來說還是可見的(只是不能獲取焦點能事件),***所以不會執行 LifecycleListActivity - onStop() 方法 *** 。

按照慣例,附上測試的Log資訊:

04-29 16:40:39.033 16188-16188/com.burjal.androidcomponent I/LifecycleListActivity: ==[onPause]==
04-29 16:40:39.053 16188-16188/com.burjal.androidcomponent I/LifecycleTransActivity: ==[onCreate]==
04-29 16:40:39.083 16188-16188/com.burjal.androidcomponent I/LifecycleTransActivity: ==[onStart]==
04-29 16:40:39.083 16188-16188/com.burjal.androidcomponent I/LifecycleTransActivity: ==[onResume]==
複製程式碼

Q4: 從 TransActivity2 返回 Activity1 問題

** Question:描述 LifecycleTransActivity 返回到 LifecycleListActivity ,生命週期變化過程?**

** Answer:**

  • onBackPressed() 流程: LifecycleTransActivity - onPause() --- LifecycleListActivity - onResume() --- LifecycleTransActivity - onStop() --- LifecycleTransActivity - onDestroy()

從透明風格的 TransActivity 返回到 Activity,生命週期變化基本同 Q2(普通 Activity 返回問題)。差別就是,此時返回不需要走 onRestart() - onStart() 流程,直接進入返回 ActivityonResume() 方法。

同樣,附上測試的Log資訊,有Log有真相:

04-29 16:59:34.033 16188-16188/com.burjal.androidcomponent I/LifecycleTransActivity: ==[onPause]==
04-29 16:59:34.043 16188-16188/com.burjal.androidcomponent I/LifecycleListActivity: ==[onResume]==
04-29 16:59:34.063 16188-16188/com.burjal.androidcomponent I/LifecycleTransActivity: ==[onStop]==
04-29 16:59:34.063 16188-16188/com.burjal.androidcomponent I/LifecycleTransActivity: ==[onDestroy]==
複製程式碼

End

下面附上 Activity 各生命週期官方解釋:

  • onCreate(Bundle savedInstanceState) : Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById() to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri , String[], String, String[], String) to retrieve cursors for data being displayed, etc.
  • onResume() : Called after onRestoreInstanceState(), onRestart(), or onPause(), for your activity to start interacting with the user.This is a good place to begin animations, open exclusive-access devices(such as the camera), etc.
  • onStart() : Called after onCreate(); or after onRestart() when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume().
  • onRestart() : Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().
  • onPause() : Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
  • onStop() : Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing,depending on later user activity.
  • onDestroy() :Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

當然,在 Android 優化中,還有很多問題需要去繼續探索,這篇文章主要是分享關於 Activity 切換的生命週期問題

相關文章