Android Application Model Lab - Android AP實驗室 (Part1)

 
出發點:
從不寫程式,到開始學習 Android programming ,發現上層 AP 跟以前 sequential language (C, Pascal) 的邏輯概念還真的大不相同。為了探索 Android application model 的一些特性,所以準備設計一些小實驗,希望可以解決一些思考的瓶頸。

Activity lifecycle (個人找到最仔細的,請參考附錄的 URL)


Case1: Activity lifecycle (新的 SDK 出現了 Fragments 概念,還沒有 study)


  • Test1: (Home key 作用)

  1. 開啟 AP: PID381 / onCreate1 -> onStart1 -> onResume1
  2. 回 HOME: PID381 / onPause1 -> onStop1
  • Test2: (Return key 作用)
    1. 重啟 AP: PID381 / onRestart1 -> onStart2 -> onResume2
    2. Return key: PID381 / onPause2 -> onStop2 -> onDestroy1
    • Test3: (Kill process 作用)
      1. 重開 AP: PID561 / onCreate1 -> onStart1 -> onResume1
      2. Kill AP: None
      • Test4: (Suspend / Resume / Shutdown 作用)
        1. 重開 AP: PID1596 / onCreate1 -> onStart1 -> onResume1
        2. Suspend: onPause1
        3. Resume: onResume2
        4. Shutdown down by power key: onPause2
        5. Reboot: None
        • Test5: (Suspend / Shutdown 作用)
        1. 重開 AP: onCreate1 -> onStart1 -> onResume1
        2. Suspend: onPause1
        3. Shutdown by power key: onResume2 -> onPause2
        4. Reboot: None
        • Test6: (Screen rotation 作用)
        1. 重開 AP: onCreate1 -> onStart1 -> onResume1
        2. 螢幕轉向 (Portrait -> Landscape): onPause1 -> onStop1 -> onDestroy1 -> onCreate1 -> onStart1 -> onResume1
        3. Suspend: onPause1 -> onStop1 -> onDestroy1 -> onCreate1 -> onStart1 -> onResume1 -> onPause1
        4. Resume: onResume2
        5. Suspend: onPause2
        6. 螢幕轉向 (Landscape -> Portrait): onResume3

        Case1 的觀察:
        • 在 Case1 的狀況下,PID 都會一樣,除非用 3rd party tool 把 Process kill 掉。
        • Return key 之後,Activity (Task) 被摧毀了 (onDestroy),但是 Kernel Process 一直存在 (By ps command)。除非用 3rd party 程式把 Process kill 掉才會消失,要不然會一直在系統中。
        • Press Home key 只會將程式暫時放到 onStop。
        • 螢幕轉向會重跑整個 Activity lifecycle,自然速度會變慢。
        • 從 lifecycle 路徑和各家前輩的推薦,任何要暫存的資料,都要放在 onPause,這樣進出才不會有問題。
        • AP 的 androidmenifest.xml 通常會宣告 android.intent.action.MAIN & android.intent.category.LAUNCHER, 這代表是程式的主進入點,其 activity 屬性為 singleTask & singleInstance。(預計有延伸實驗)
        • onPause() 觸發條件:
          • System shutdown by power key
          • Suspend
          • Alarm
        • onStop() 觸發條件:
          • Back to HOME
          • Phone call interrupt
          • Camera on
          • AP switch (等價於 Back to HOME)
          • Intent another Activity
        • onDestroy() 觸發條件:
          • Press Return key
          • Screen rotate
         
        參考資料:
        arrow
        arrow
          全站熱搜
          創作者介紹
          創作者 丘猴子 的頭像
          丘猴子

          轉貼部落格

          丘猴子 發表在 痞客邦 留言(0) 人氣()