{"id":7844,"date":"2024-07-03T21:01:01","date_gmt":"2024-07-03T13:01:01","guid":{"rendered":""},"modified":"2024-07-03T21:01:01","modified_gmt":"2024-07-03T13:01:01","slug":"\u5b89\u5353\u4e2dactivity\u7684\u751f\u547d\u5468\u671f_Android\u751f\u547d\u5468\u671f","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7844.html","title":{"rendered":"\u5b89\u5353\u4e2dactivity\u7684\u751f\u547d\u5468\u671f_Android\u751f\u547d\u5468\u671f"},"content":{"rendered":"
\u76ee\u5f55<\/strong><\/p>\n Activity \u7b80\u4ecb<\/p>\n Activity \u751f\u547d\u5468\u671f\u6982\u5ff5<\/p>\n onCreate()<\/p>\n onStart()<\/p>\n onResume()<\/p>\n onPouse()<\/p>\n onStop()<\/p>\n onDestory()<\/p>\n onRestart()<\/p>\n Activity \u751f\u547d\u5468\u671f\u8d85\u65f6\u8bbe\u5b9a <\/p>\n \u4fdd\u5b58\u548c\u6062\u590d\u77ac\u65f6\u754c\u9762\u72b6\u6001<\/p>\n onSaveInstanceState()\u4f7f\u7528<\/p>\n \u5728 Activity \u4e4b\u95f4\u5bfc\u822a<\/p>\n \u591a\u7a97\u53e3\u652f\u6301<\/p>\n Framework\u542f\u52a8activity \u6d41\u7a0b<\/p>\n \u4e00\u822c\u6d41\u7a0b<\/p>\n framework\u901a\u77e5activity\u751f\u547d\u5468\u671f-ActivityLifecycleItem<\/p>\n \u7c7b\u56fe<\/p>\n \u6d41\u7a0b\u56fe<\/p>\n \u53c2\u8003\u6587\u6863<\/p>\n \u5f53\u7528\u6237\u6d4f\u89c8\u3001\u9000\u51fa\u548c\u8fd4\u56de\u5e94\u7528\u65f6\uff0c\u5e94\u7528\u4e2d\u7684 Activity \u7c7b\u63d0\u4f9b\u516d\u4e2a\u6838\u5fc3\u56de\u8c03\uff1a <\/p>\n \u4e0a\u9762\u56fe\u6982\u62ec\u4e86android\u751f\u547d\u5468\u671f\u7684\u5404\u4e2a\u73af\u8282\uff0c\u63cf\u8ff0\u4e86activity\u4ece\u751f\u6210\u5230\u9500\u6bc1\u7684\u8fc7\u7a0b\u3002<\/p>\n \u5f53\u6211\u4eec\u70b9\u51fbactivity\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528activity\u7684oncreate()\u65b9\u6cd5\uff0cactivity\u5fc5\u987b\u5b9e\u73b0\u6b64\u56de\u8c03\uff0c\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u6211\u4eec\u4f1a\u4f7f\u7528 onCreate()\u65b9\u6cd5\u5b8c\u6210\u540e\uff0c\u6b64\u65f6activity\u8fdb\u5165onStart()\u65b9\u6cd5,\u5f53\u524dactivity\u662f\u7528\u6237\u53ef\u89c1\u72b6\u6001\uff0c\u4f46\u6ca1\u6709\u7126\u70b9\uff0c\u4e0e\u7528\u6237\u4e0d\u80fd\u4ea4\u4e92\uff0c\u4e00\u822c\u53ef\u5728\u5f53\u524d\u65b9\u6cd5\u505a\u4e00\u4e9b\u52a8\u753b\u7684\u521d\u59cb\u5316\u64cd\u4f5c\u3002<\/p>\n onStart()\u65b9\u6cd5\u5b8c\u6210\u4e4b\u540e\uff0c\u6b64\u65f6activity\u8fdb\u5165onResume()\u65b9\u6cd5\u4e2d\uff0c\u5f53\u524dactivity\u72b6\u6001\u5c5e\u4e8e\u8fd0\u884c\u72b6\u6001 (Running)\uff0c\u53ef\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u3002activity \u9700\u8981\u6062\u590d\u5728onPause \u51fd\u6570\u4e2d\u91ca\u653e\u6389\u7684\u7ec4\u4ef6\u3002<\/p>\n \u5f53\u53e6\u5916\u4e00\u4e2aactivity\u8986\u76d6\u5f53\u524d\u7684acitivty\u65f6\uff0c\u6b64\u65f6\u5f53\u524dactivity\u4f1a\u8fdb\u5165\u5230onPouse()\u65b9\u6cd5\u4e2d\uff0c\u5f53\u524dactivity\u662f\u53ef\u89c1\u7684\uff0c\u4f46\u4e0d\u80fd\u4e0e\u7528\u6237\u4ea4\u4e92\u72b6\u6001\u3002\u6b64\u65b9\u6cd5\u8868\u793a Activity \u4e0d\u518d\u4f4d\u4e8e\u524d\u53f0\uff08\u5c3d\u7ba1\u5728\u7528\u6237\u5904\u4e8e\u591a\u7a97\u53e3\u6a21\u5f0f\u65f6 Activity \u4ecd\u7136\u53ef\u89c1\uff09\u3002<\/p>\n Activity \u8fdb\u5165\u6b64\u72b6\u6001\u7684\u539f\u56e0\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff1a<\/p>\n \u5982\u679c\u5904\u4e8e\u591a\u7a97\u53e3\u6a21\u5f0f\uff0c\u201c\u5df2\u6682\u505c\u201d\u7684 Activity \u4ecd\u5b8c\u5168\u53ef\u89c1\u3002\u56e0\u6b64\uff0c\u5e94\u8be5\u8003\u8651\u4f7f\u7528 onStop() \u800c\u975e onPause() \u6765\u5b8c\u5168\u91ca\u653e\u6216\u8c03\u6574\u4e0e\u754c\u9762\u76f8\u5173\u7684\u8d44\u6e90\u548c\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u652f\u6301\u591a\u7a97\u53e3\u6a21\u5f0f\u3002<\/p>\n onPouse()\u65b9\u6cd5\u5b8c\u6210\u4e4b\u540e\uff0c\u6b64\u65f6activity\u8fdb\u5165onStop()\u65b9\u6cd5\uff0c\u6b64\u65f6activity\u5bf9\u7528\u6237\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u5728\u7cfb\u7edf\u5185\u5b58\u7d27\u5f20\u7684\u60c5\u51b5\u4e0b\uff0c\u6709\u53ef\u80fd\u4f1a\u88ab\u7cfb\u7edf\u8fdb\u884c\u56de\u6536\u3002\u6240\u4ee5\u4e00\u822c\u5728\u5f53\u524d\u65b9\u6cd5\u53ef\u505a\u8d44\u6e90\u56de\u6536\u3002<\/p>\n \u5982\u679c Activity \u5df2\u7ed3\u675f\u8fd0\u884c\u5e76\u5373\u5c06\u7ec8\u6b62\uff0c\u7cfb\u7edf\u8fd8\u53ef\u4ee5\u8c03\u7528 \u5e94\u7528\u5e94\u91ca\u653e\u6216\u8c03\u6574\u5728\u5e94\u7528\u5bf9\u7528\u6237\u4e0d\u53ef\u89c1\u65f6\u7684\u65e0\u7528\u8d44\u6e90\u3002\u4f8b\u5982\uff0c\u5e94\u7528\u53ef\u4ee5\u6682\u505c\u52a8\u753b\u6548\u679c\uff0c\u6216\u4ece\u7cbe\u786e\u4f4d\u7f6e\u66f4\u65b0\u5207\u6362\u5230\u7c97\u7565\u4f4d\u7f6e\u66f4\u65b0\u3002\u4f7f\u7528 \u8fd8\u5e94\u4f7f\u7528 \u8fdb\u5165\u201c\u5df2\u505c\u6b62\u201d\u72b6\u6001\u540e\uff0cActivity \u8981\u4e48\u8fd4\u56de\u4e0e\u7528\u6237\u4e92\u52a8\uff0c\u8981\u4e48\u7ed3\u675f\u8fd0\u884c\u5e76\u6d88\u5931\u3002\u5982\u679c Activity \u8fd4\u56de\uff0c\u7cfb\u7edf\u5c06\u8c03\u7528 \u9500\u6bc1 Ativity \u4e4b\u524d\uff0c\u7cfb\u7edf\u4f1a\u5148\u8c03\u7528 onDestroy()\u3002\u7cfb\u7edf\u8c03\u7528\u6b64\u56de\u8c03\u7684\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n \u5982\u679c Activity \u5373\u5c06\u7ed3\u675f\uff0conDestroy() \u662f Activity \u6536\u5230\u7684\u6700\u540e\u4e00\u4e2a\u751f\u547d\u5468\u671f\u56de\u8c03\u3002\u5982\u679c\u7531\u4e8e\u914d\u7f6e\u53d8\u66f4\u800c\u8c03\u7528 onDestroy()\uff0c\u7cfb\u7edf\u4f1a\u7acb\u5373\u65b0\u5efa Activity \u5b9e\u4f8b\uff0c\u7136\u540e\u5728\u65b0\u914d\u7f6e\u4e2d\u4e3a\u65b0\u5b9e\u4f8b\u8c03\u7528 onCreate()\u3002<\/p>\n onDestroy() \u56de\u8c03\u5e94\u91ca\u653e\u5148\u524d\u7684\u56de\u8c03\uff08\u4f8b\u5982 onStop()\uff09\u5c1a\u672a\u91ca\u653e\u7684\u6240\u6709\u8d44\u6e90\u3002<\/p>\n \u5e94\u4f7f\u7528 ViewModel \u5bf9\u8c61\u6765\u5305\u542b Activity \u7684\u76f8\u5173\u89c6\u56fe\u6570\u636e\uff0c\u800c\u4e0d\u662f\u5728 Activity \u4e2d\u52a0\u5165\u903b\u8f91\u6765\u786e\u5b9a Activity \u88ab\u9500\u6bc1\u7684\u539f\u56e0\u3002\u5982\u679c\u56e0\u914d\u7f6e\u53d8\u66f4\u800c\u91cd\u65b0\u521b\u5efa Activity\uff0cViewModel \u4e0d\u5fc5\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u56e0\u4e3a\u7cfb\u7edf\u5c06\u4fdd\u7559 ViewModel \u5e76\u5c06\u5176\u63d0\u4f9b\u7ed9\u4e0b\u4e00\u4e2a Activity \u5b9e\u4f8b\u3002\u5982\u679c\u4e0d\u91cd\u65b0\u521b\u5efa Activity\uff0cViewModel \u5c06\u8c03\u7528 onCleared() \u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5728 Activity \u88ab\u9500\u6bc1\u524d\u6e05\u9664\u6240\u9700\u7684\u4efb\u4f55\u6570\u636e\u3002<\/p>\n onRestart()\u65b9\u6cd5\u5728\u7528\u6237\u6309\u4e0bhome()\u4e4b\u540e\uff0c\u518d\u6b21\u8fdb\u5165\u5230\u5f53\u524dactivity\u7684\u65f6\u5019\u8c03\u7528\u3002\u8c03\u7528\u987a\u5e8fonPouse()->onStop()->onRestart()\u2192onStart()\u2192onResume().<\/p>\n<\/p>\n \u5728ActiviteStack.java\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u751f\u547d\u5468\u671f\u7684\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n \u67d0\u4e9b\u60c5\u51b5\u7528\u6237\u5e0c\u671b\u5f53\u4ed6\u4eec\u542f\u52a8 Activity \u65f6\uff0c\u8be5 Activity \u7684\u4e0a\u6b21\u7684\u754c\u9762\u72b6\u6001\u4f1a\u4fdd\u6301\u4e0d\u53d8\uff0c\u76f4\u5230\u7528\u6237\u5b8c\u5168\u5173\u95ed Activity \u4e3a\u6b62\u3002\u4f8b\u5982\u914d\u7f6e\u6539\u53d8\u5bfc\u81f4activity \u91cd\u542f\u3001\u7528\u6237\u6682\u65f6\u5207\u6362\u5230\u5176\u4ed6\u5e94\u7528\uff0c\u7a0d\u540e\u518d\u8fd4\u56de\u5230\u5e94\u7528\uff0c\u4ed6\u4eec\u4e5f\u4f1a\u5e0c\u671b Activity \u7684\u754c\u9762\u72b6\u6001\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n \u5f53 Activity \u56e0\u7cfb\u7edf\u9650\u5236\u800c\u88ab\u9500\u6bc1\u65f6\uff0c\u53ef\u7ec4\u5408\u4f7f\u7528\u4e00\u4e0b\u65b9\u6cd5\u6765\u4fdd\u7559\u7528\u6237\u7684\u77ac\u65f6\u754c\u9762\u72b6\u6001\uff1a<\/p>\n \u5f53Activity \u5f00\u59cb\u505c\u6b62\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528 onSaveInstanceState() \u65b9\u6cd5\uff0c\u4ee5\u4fbfActivity \u53ef\u4ee5\u5c06\u72b6\u6001\u4fe1\u606f\u4fdd\u5b58\u5230\u5b9e\u4f8b\u72b6\u6001 Bundle \u4e2d\u3002\u6b64\u65b9\u6cd5\u7684\u9ed8\u8ba4\u5b9e\u73b0\u4fdd\u5b58\u6709\u5173 Activity \u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u72b6\u6001\u7684\u77ac\u65f6\u4fe1\u606f\uff0c\u4f8b\u5982 EditText \u5fae\u4ef6\u4e2d\u7684\u6587\u672c\u6216 ListView \u5fae\u4ef6\u7684\u6eda\u52a8\u4f4d\u7f6e\u3002<\/p>\n \u5982\u9700\u4fdd\u5b58 Activity \u7684\u5176\u4ed6\u5b9e\u4f8b\u72b6\u6001\u4fe1\u606f\uff0c\u5fc5\u987b\u66ff\u6362 onSaveInstanceState()\uff0c\u5e76\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u5230 Bundle \u5bf9\u8c61\u4e2d\u3002\u66ff\u6362 onSaveInstanceState() \u65f6\uff0c\u5982\u679c\u671b\u9ed8\u8ba4\u5b9e\u73b0\u4fdd\u5b58\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u7684\u72b6\u6001\uff0c\u5fc5\u987b\u8c03\u7528\u7236\u7c7b\u5b9e\u73b0\u3002<\/p>\n onSaveInstanceState\u65b9\u6cd5\u8c03\u7528\u6ce8\u610f\u4e8b\u9879 :<\/strong> \u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000Android P \u4e4b\u524d\uff0c\u3000onSaveInstanceState\u5728onStop()\u524d\u8c03\u7528\u3002<\/p>\n \u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000Android P\u53ca\u4e4b\u540e onSaveInstanceState\u5728onStop()\u540e\u8c03\u7528\u3002<\/p>\n<\/p>\n \u4ece\u4e00\u4e2a Activity \u542f\u52a8\u53e6\u4e00\u4e2a Activity<\/p>\n \u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u542f\u52a8activity:<\/p>\n 1. 2. \u53ef\u4f7f\u7528adb logcat -b events\u3000\u67e5\u770b\u751f\u547d\u5468\u671f\u8c03\u7528\u987a\u5e8f<\/p>\n \u6ce8\uff11:<\/strong>\u3000\u5f53AActivity\u5207\u6362BActivity\u7684\u6240\u6267\u884c\u7684\u65b9\u6cd5\uff1a<\/p>\n AActivity:\u3000onCreate()->onStart()->onResume()->onPouse() \u6ce8\uff12:\u3000<\/strong>\u4eceBActivity \u6309back \u9000\u56deAActivity\uff1a<\/p>\n BActivity\uff1aonPouse() \u6ce8 3:\u3000<\/strong>\u5f53AActivity\u5207\u6362BActivity\uff08\u6b64activity\u662f\u4ee5dialog\u5f62\u5f0f\u5b58\u5728\u7684\uff09\u6240\u6267\u884c\u7684\u65b9\u6cd5:<\/p>\n AActivity: onCreate()->onStart()->onResume()->onPouse() \u6ce8 4:\u3000<\/strong>AActivity\u6309power \u706d\u5c4f<\/p>\n AActivity:\u3000onPause() \u2192 onStop()<\/p>\n keyguard show<\/p>\n \u6ce8 5:\u3000<\/strong>keyguard \u5230AActivity\uff1a<\/p>\n keyguard go away<\/p>\n AActivity: onRestart()\u2192 onStart()\u2192onResume()<\/p>\n \u6ce8 6:<\/strong>\u3000AActivity\u4e2d\u4e0b\u62c9\u5bfc\u822a\u680f\uff1a<\/p>\n AActivity \u5931\u53bb\u7126\u70b9\uff0c\u4f9d\u7136resume\u72b6\u6001<\/p>\n Android 7.0 \u65b0\u589e\u4e86\u5bf9\u540c\u65f6\u663e\u793a\u591a\u4e2a\u5e94\u7528\u7a97\u53e3\u7684\u652f\u6301\u3002\u5206\u5c4f\uff0c\u3000\u753b\u4e2d\u753b\uff0c\u3000freeform\u3002\u3000<\/p>\n \u591a\u7a97\u53e3\u6a21\u5f0f\u4e0d\u4f1a\u66f4\u6539 activity \u751f\u547d\u5468\u671f\u3002<\/p>\n \u4e0d\u540cAndroid\u7248\u672c\u533a\u522b\uff1a<\/p>\n \u4ee5\u4e0b\u4ec5\u4e3a\u666e\u904d\u6d41\u7a0b\uff0c \u4e0d\u540c\u60c5\u51b5\u4e0b\uff0c \u542f\u52a8activity \u6d41\u7a0b\u7565\u6709\u4e0d\u540c\uff0c \u6bd4\u5982\u6e29\u542f\u52a8\u7b49\u3002\u4ec5\u4f9b\u53c2\u8003\u3002 call stack \u4e3a\u4ece\u4e0b\u5230\u4e0a\u3002 <\/p>\n 1. \u542f\u52a8\u4e4b\u524d\u5df2\u7ecf\u542f\u52a8\u8fc7\uff0c\u4e14\u6ca1\u6709\u88abdestroy\u7684activity, \u5148resume target activity, \u7136\u540epause \u5f53\u524dactivity:<\/p>\n startPausingLocked:1102, ActivityStack (com.android.server.wm) 2. \u542f\u52a8\u65b0\u7684activity<\/p>\n \u5148\u521b\u5efaactivity process, paused \u5f53\u524dactivity. <\/p>\n startPausingLocked:1102, ActivityStack (com.android.server.wm) resumeTopActivityInnerLocked:1662, ActivityStack (com.android.server.wm) resumeTopActivityUncheckedLocked:1507, ActivityStack (com.android.server.wm) resumeFocusedStacksTopActivities:2362, RootWindowContainer (com.android.server.wm) startActivityInner:1739, ActivityStarter (com.android.server.wm) startActivityUnchecked:1528, ActivityStarter (com.android.server.wm) executeRequest:1187, ActivityStarter (com.android.server.wm) execute:669, ActivityStarter (com.android.server.wm) startActivityAsUser:1145, ActivityTaskManagerService (com.android.server.wm) startActivityAsUser:1117, ActivityTaskManagerService (com.android.server.wm) startActivity:1092, ActivityTaskManagerService (com.android.server.wm) onTransact:1429, IActivityTaskManager$Stub (android.app) execTransactInternal:1154, Binder (android.os) execTransact:1123, Binder (android.os)<\/p>\n<\/p>\n \u7136\u540e\u5728\u6536\u5230activityPaused\u540eresume\u65b0\u7684activity\uff0c activityPause\uff1a<\/p>\n framework \u901a\u8fc7activitylifecycleitem \u53d1\u9001activity \u751f\u547d\u5468\u671f\u72b6\u6001\u7ed9activity,\u5bf9\u5e94pause, resume \u7b49\u751f\u547d\u5468\u671f\uff0c ActivityLifecycleItem \u7ee7\u627f\u81eaClientTransactionItem\uff0c\u8fd9\u4e9bactivitylifecycleitem\u5305\u62ec\uff1a <\/p>\n StartActivityItem \u5176\u4ed6\u7ee7\u627fClientTransactionItem\u7684\u7c7b\uff1a \u5728framework\u4e2d\u901a\u8fc7\u7c7b\u4f3c\u4ee5\u4e0b\u4ee3\u7801\u53d1\u9001\u751f\u547d\u5468\u671f\u4fe1\u606f\u7ed9app:<\/p>\n 1. ClientTransaction\u6301\u6709IApplicationThread\uff0c\u4e3a\u8981\u901a\u4fe1\u7684app\u7aefAIDL\u63a5\u53e3\uff0c \u670d\u52a1\u7aef\u901a\u8fc7\u8be5\u63a5\u53e3\u4e8eapp\u7aef\u901a\u4fe1\u3002ClientTransaction\u6301\u6709\u5bf9\u8c61mActivityCallbacks\u548cmLifecycleStateRequest\uff0c\u8fd9\u4e9b\u5747\u4e3aClientTransactionItem\u7c7b\u578b\u3002 mActivityCallbacks\u4e3alist\uff0c \u53ef\u4ee5\u6709\u591a\u4e2a\u3002 mLifecycleStateRequest\u53ea\u80fd\u6709\u4e00\u4e2a\u3002<\/p>\n 2.ClientTransactionItem\u5b50\u7c7b\u5177\u4f53\u63cf\u8ff0\u8981\u6c42app\u6267\u884c\u7684\u751f\u547d\u5468\u671f\u72b6\u6001\u6216\u5176\u4ed6\u4fe1\u606f\uff0c \u6709\u591a\u4e2a\u5b50\u7c7b\u3002 \u5176\u4e2dActivityLifecycleItem\u5b50\u7c7b\u4e13\u95e8\u7528\u6765\u63cf\u8ff0\u8981\u6c42app\u6267\u884c\u7684\u751f\u547d\u5468\u671f\u72b6\u6001\u3002 <\/p>\n 3. ActivityThread\u7ee7\u627fClientTransactionHandler\uff0c ClientTransactionItem\u8c03\u7528\u8be5\u7c7b\u6267\u884c\u76f8\u5173\u529f\u80fd\uff0c \u5982handleResumeActivity()\u3002<\/p>\n <\/p>\n 1.\u5728service\u4fa7\uff0c\u9996\u5148\u4f1a\u6784\u5efa\u4e00\u4e2aClientTransaction\u5bf9\u8c61\uff0c \u901a\u8fc7addCallback()\u8bbe\u7f6emActivityCallbacks\u548c\u901a\u8fc7setLifecycleStateRequest()\u8bbe\u7f6emLifecycleStateRequest\u3002 \u5176\u4e2dmActivityCallbacks\u4e3alist\uff0c \u53ef\u4ee5\u6709\u591a\u4e2a\u3002 mLifecycleStateRequest\u53ea\u80fd\u6709\u4e00\u4e2a\uff0c \u4e8c\u8005\u5747\u4e3aClientTransactionItem\u5b50\u7c7b\u3002 <\/p>\n 2. ams \u548cwms\u5728\u9002\u5f53\u65f6\u673a\u901a\u8fc7\u5728ClientLifecycleManager.scheduleTransaction\u5c06\u4e0a\u4e00\u6b65\u521b\u5efa\u7684ClientTransaction\u4f20\u9012\u7ed9app, \u901a\u77e5app\u751f\u547d\u5468\u671f\u72b6\u6001\u6216\u8005\u5176\u4ed6\u4fe1\u606f\u53d8\u5316<\/p>\n 2.\u5728app \u7684ActivityThread\u4e2d\u63a5\u6536\u5230\u8fd9\u4e9b\u53d8\u5316\u540e\uff0c \u5148\u8c03\u7528ClientTransaction\u7684mActivityCallbacks\u7684preExecute\u540e\u8c03\u7528mLifecycleStateRequest\u7684preExecute()\u8bbe\u7f6e\u4e00\u4e9b\u63d0\u524d\u5de5\u4f5c\u3002\u8fd9\u4e9b\u5747\u4e3aClientTransactionItem\u5b50\u7c7b\uff0c \u6bcf\u4e2a\u5b50\u7c7b\u7684\u6267\u884c\u4e0d\u540c\uff0c\u5305\u62ec\u540e\u9762\u7684execute()\u548cpostEcexute()\u5404\u5b50\u7c7b\u6709\u4e0d\u540c\u5b9e\u73b0\u3002 \u8c03\u7528\u987a\u5e8f\u4e5f\u4e3a\u5148mActivityCallbacks\u540emLifecycleStateRequest\u3002 \u5982\u5728ConfigurationChangeItem\u4e2d\u9884\u7f6econfiguration,\u5728 DestroyActivityItem\u4e2d\u6dfb\u52a0destroyactivity\u5230ActivityThread\u7684mActivitiesToBeDestroyed\u5217\u8868\u3002\u8c03\u7528\u987a\u5e8f\u4e3a\u5148\u8c03\u7528mActivityCallbacks\u7684preExecute\u540e\u8c03\u7528mLifecycleStateRequest\u7684preExecute(). <\/p>\n 3.\u5728app \u7aef\u8c03\u7528ClientTransactionItem\u5b50\u7c7b\u7684execute() \u6267\u884c\u72b6\u6001\u6216\u5176\u4ed6\u4fe1\u606f\u53d8\u5316. \u6bd4\u5982\u8c03\u7528\u5230activity.onResume\u7b49\u51fd\u6570\u3002 <\/p>\n 4.\u5728app \u7aef\u8c03\u7528ClientTransactionItem\u5b50\u7c7b\u7684postEcexute()\u901a\u77e5service\uff0c \u4e00\u822c\u4e3aActivityTaskManager\u6267\u884c\u4e86\u76f8\u5173\u64cd\u4f5c\uff0c\u5982activityPaused(), \u4ece\u800c\u5728services\u7aef\u7ee7\u7eed\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\uff0c \u5e76\u53bb\u6389\u7cfb\u7edf\u7684timeout .<\/p>\n <\/p>\n https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle<\/p>\n
\n
\nActivity \u7b80\u4ecb<\/h3>\n
Activity<\/code> \u7c7b\u662f Android \u5e94\u7528\u7684\u5173\u952e\u7ec4\u4ef6\uff0cActivity \u5145\u5f53\u4e86\u5e94\u7528\u4e0e\u7528\u6237\u4e92\u52a8\u7684\u5165\u53e3\u70b9\u3002\u901a\u5e38\uff0c\u4e00\u4e2a Activity \u5b9e\u73b0\u5e94\u7528\u4e2d\u7684\u4e00\u4e2a\u754c\u9762\u3002<\/p>\n<\/p>\n
Activity \u751f\u547d\u5468\u671f\u6982\u5ff5<\/h3>\n
Activity<\/code> \u5b9e\u4f8b\u4f1a\u5728\u5176\u751f\u547d\u5468\u671f\u7684\u4e0d\u540c\u72b6\u6001\u95f4\u8f6c\u6362\u3002
Activity<\/code> \u7c7b\u4f1a\u63d0\u4f9b\u8bb8\u591a\u56de\u8c03\uff0c\u8fd9\u4e9b\u56de\u8c03\u4f1a\u8ba9 Activity \u77e5\u6653\u67d0\u4e2a\u72b6\u6001\u5df2\u7ecf\u66f4\u6539\uff1a\u7cfb\u7edf\u6b63\u5728\u521b\u5efa\u3001\u505c\u6b62\u6216\u6062\u590d\u67d0\u4e2a Activity\uff0c\u6216\u8005\u6b63\u5728\u9500\u6bc1\u8be5 Activity \u6240\u5728\u7684\u8fdb\u7a0b\u3002\u4f7f\u7528\u8fd9\u4e9b\u56de\u8c03Activity\u53ef\u4ee5\u5728\u9002\u5f53\u7684\u65f6\u673a\u505a\u76f8\u5e94\u7684\u5de5\u4f5c\u3002<\/p>\n
onCreate()<\/code>\u3001
onStart()<\/code>\u3001
onResume()<\/code>\u3001
onPause()<\/code>\u3001
onStop()<\/code> \u548c
onDestroy()<\/code>\u3002<\/p>\n
onCreate()<\/strong><\/h4>\n
setContentView()<\/code> \u6765\u6307\u5b9a XML \u5e03\u5c40\u6587\u4ef6\u3002 \u5b83\u4f1a\u5728\u7cfb\u7edf\u9996\u6b21\u521b\u5efa Activity \u65f6\u89e6\u53d1\u3002Activity \u4f1a\u5728\u521b\u5efa\u540e\u8fdb\u5165\u201c\u5df2\u521b\u5efa\u201d\u72b6\u6001\u3002\u5728
onCreate()<\/code> \u65b9\u6cd5\u4e2d\uff0c\u9700\u6267\u884c\u57fa\u672c\u5e94\u7528\u542f\u52a8\u903b\u8f91\uff0c\u8be5\u903b\u8f91\u5728 Activity \u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u4e2d\u53ea\u5e94\u53d1\u751f\u4e00\u6b21\u3002\u4f8b\u5982\uff0c\u5c06\u6570\u636e\u7ed1\u5b9a\u5230\u5217\u8868, \u5b9e\u4f8b\u5316\u67d0\u4e9b\u7c7b\u53d8\u91cf\u3002\u6b64\u65b9\u6cd5\u4f1a\u63a5\u6536
savedInstanceState<\/code> \u53c2\u6570\uff0c\u540e\u8005\u662f\u5305\u542b Activity \u5148\u524d\u4fdd\u5b58\u72b6\u6001\u7684
Bundle<\/code> \u5bf9\u8c61\u3002\u5982\u679c Activity \u6b64\u524d\u672a\u66fe\u5b58\u5728\uff0c
Bundle<\/code> \u5bf9\u8c61\u7684\u503c\u4e3a null\u3002<\/p>\n
onStart()<\/strong><\/h4>\n
onResume()<\/strong><\/h4>\n
onPouse()<\/strong><\/h4>\n
\n
onPause()<\/code> \u6267\u884c\u975e\u5e38\u7b80\u5355\uff0c\u800c\u4e14\u4e0d\u4e00\u5b9a\u8981\u6709\u8db3\u591f\u7684\u65f6\u95f4\u6765\u6267\u884c\u4fdd\u5b58\u64cd\u4f5c, \u5728AMS\u4e2dpause time out \u65f6\u95f4\u4e3a500ms\u3002\u56e0\u6b64\uff0c\u4e0d<\/strong>\u5e94\u4f7f\u7528
onPause()<\/code> \u6765\u4fdd\u5b58\u5e94\u7528\u6216\u7528\u6237\u6570\u636e\u3001\u8fdb\u884c\u7f51\u7edc\u8c03\u7528\u6216\u6267\u884c\u6570\u636e\u5e93\u4e8b\u52a1\u3002\u56e0\u4e3a\u5728\u8be5\u65b9\u6cd5\u5b8c\u6210\u4e4b\u524d\uff0c\u6b64\u7c7b\u5de5\u4f5c\u53ef\u80fd\u65e0\u6cd5\u5b8c\u6210\u3002\u76f8\u53cd\uff0c\u5e94\u5728
onStop()<\/code> \u671f\u95f4\u6267\u884c\u9ad8\u8d1f\u8f7d\u7684\u5173\u95ed\u64cd\u4f5c\u3002<\/p>\n
onStop()<\/strong><\/h4>\n
onStop()<\/code>\u3002<\/p>\n
onStop()<\/code> \u800c\u975e
onPause()<\/code> \u53ef\u786e\u4fdd\u4e0e\u754c\u9762\u76f8\u5173\u7684\u5de5\u4f5c\u7ee7\u7eed\u8fdb\u884c\uff0c\u5373\u4f7f\u7528\u6237\u5728\u591a\u7a97\u53e3\u6a21\u5f0f\u4e0b\u67e5\u770bActivity \u4e5f\u80fd\u5982\u6b64\u3002<\/p>\n
onStop()<\/code> \u6267\u884c CPU \u76f8\u5bf9\u5bc6\u96c6\u7684\u5173\u95ed\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u65e0\u6cd5\u627e\u5230\u66f4\u5408\u9002\u7684\u65f6\u673a\u6765\u5c06\u4fe1\u606f\u4fdd\u5b58\u5230\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u5728
onStop()<\/code> \u671f\u95f4\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/p>\n
onRestart()<\/code>\u3002\u5982\u679c
Activity<\/code> \u7ed3\u675f\u8fd0\u884c\uff0c\u7cfb\u7edf\u5c06\u8c03\u7528
onDestroy()<\/code>\u3002<\/p>\n
onDestory()<\/strong><\/h4>\n
\n
onRestart()<\/strong><\/h4>\n
Activity \u751f\u547d\u5468\u671f\u8d85\u65f6\u8bbe\u5b9a <\/h3>\n
\n
\/ How long we wait until giving up on the last activity to pause. This \/\/ is short because it directly impacts the responsiveness of starting the \/\/ next activity. private static final int PAUSE_TIMEOUT = 500; \/\/ How long we wait for the activity to tell us it has stopped before \/\/ giving up. This is a good amount of time because we really need this \/\/ from the application in order to get its saved state. private static final int STOP_TIMEOUT = 10 * 1000; \/\/ How long we wait until giving up on an activity telling us it has \/\/ finished destroying itself. private static final int DESTROY_TIMEOUT = 10 * 1000<\/code><\/pre>\n<\/p>\n
\u4fdd\u5b58\u548c\u6062\u590d\u77ac\u65f6\u754c\u9762\u72b6\u6001<\/h3>\n
\n
ViewModel, \u53c2\u52a0\u540e\u9762\u53c2\u8003\u6587\u6863\u3002<\/code><\/li>\n
onSaveInstanceState()\uff1a\u3000\u8981\u4fdd\u7559\u7684\u662f\u7b80\u5355\u7684\u8f7b\u91cf\u7ea7\u754c\u9762\u6570\u636e\uff0c\u901a\u8fc7bundle\u952e\u503c\u5bf9\u5b58\u50a8\u6570\u636e\u3002<\/code><\/li>\n
onSaveInstanceState()\u4f7f\u7528<\/strong><\/h4>\n
\u5728activity\u518d\u6b21\u542f\u52a8\u65f6\uff0c \u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u4e4b\u4e00\u83b7\u5f97\u4e4b\u524d\u5b58\u50a8\u7684bundle\u4fe1\u606f\u3002<\/code><\/p>\n
\n
\u5728onCreate\u4e2d\u901a\u8fc7<\/code>savedInstanceState\u53c2\u6570\u4f20\u9012\u56de\u6765\uff0c\u7528\u4e8e\u6062\u590d\u76f8\u5173\u4fe1\u606f\u3002<\/li>\n
onRestoreInstanceState()\u3002\u3000<\/code>\u7cfb\u7edf\u5728 onStart() \u65b9\u6cd5\u4e4b\u540e\u8c03\u7528
onRestoreInstanceState()\uff0c<\/code>\u4ec5\u5f53\u5b58\u5728\u8981\u6062\u590d\u7684\u5df2\u4fdd\u5b58\u72b6\u6001\u65f6\uff0c\u7cfb\u7edf\u624d\u4f1a\u8c03\u7528 onRestoreInstanceState()\uff0c\u56e0\u6b64\u65e0\u9700\u68c0\u67e5 Bundle \u662f\u5426\u4e3a null\u3002\u3000<\/li>\n<\/ol>\n
\u7528\u6237\u4e3b\u52a8\u9500\u6bc1\u4e0d\u4f1a\u8c03\u7528 : \u5f53\u7528\u6237\u70b9\u51fb\u56de\u9000\u952e \u6216\u8005 \u8c03\u7528\u4e86 finish() \u65b9\u6cd5, \u4e0d\u4f1a\u8c03\u7528\u8be5\u65b9\u6cd5;
\u8c03\u7528\u65f6\u673a\u4e0d\u56fa\u5b9a : \u5728Android Honeycomb, onSaveInstanceState\u5728 onPause\u524d\u8c03\u7528\u3002<\/p>\n\u5728 Activity \u4e4b\u95f4\u5bfc\u822a<\/h3>\n
startActivity() \u542f\u52a8\u65b0\u7684activity, \u5e76\u4e0d\u9700\u8981\u65b0\u7684activity \u8fd4\u56de\u7ed3\u679c\u7ed9\u5f53\u524dactivity\u3002<\/code><\/p>\n
startActivityForResult()<\/code>\u542f\u52a8\u65b0 Activity, \u4f46\u9700\u8981
\u65b0\u7684activity \u8fd4\u56de\u7ed3\u679c\u7ed9\u5f53\u524dactivity\u3002\u7236activity \u5b9e\u73b0onActivityResult(int, int, Intent\uff09\u3002\u3000\u5b50activity \u9700\u8c03\u7528setResult(int) \u5c06\u6570\u636e\u8fd4\u56de\u5230\u5176\u7236\u7ea7\u3002<\/code><\/p>\n
BActivity:\u3000onCreate()->onStart()->onResume()
AActivity:\u3000onStop() \u53ef\u80fd\u3000\u2192onDestory()<\/p>\n
AActivity:\u3000onRestart() ->onStart()->onResume()
BActivity\uff1aonStop() \u2192onDestory()<\/p>\n
BActivity: onCreate()\u2192onStart()\u2192onResume()<\/p>\n\u591a\u7a97\u53e3\u652f\u6301<\/h3>\n
\n
Activity#onTopResumedActivityChanged()\u901a\u77e5activty \u5f97\u5230\u6216\u8005\u5931\u53bbtop resumed\u3000activity.<\/code><\/li>\n<\/ul>\n
Framework\u542f\u52a8activity \u6d41\u7a0b<\/h3>\n
\u4e00\u822c\u6d41\u7a0b<\/h4>\n
\n
pauseBackStacks:1201, TaskDisplayArea (com.android.server.wm)
resumeTopActivityInnerLocked:1658, ActivityStack (com.android.server.wm)
resumeTopActivityUncheckedLocked:1507, ActivityStack (com.android.server.wm)\/\/\u8fd4\u56detrue, \u4ee3\u8868\u5df2\u7ecfresumed activy
resumeFocusedStacksTopActivities:2362, RootWindowContainer (com.android.server.wm)
resumeTargetStackIfNeeded:2555, ActivityStarter (com.android.server.wm)
recycleTask:1947, ActivityStarter (com.android.server.wm)
startActivityInner:1634, ActivityStarter (com.android.server.wm)
startActivityUnchecked:1528, ActivityStarter (com.android.server.wm)
executeRequest:1187, ActivityStarter (com.android.server.wm)
execute:669, ActivityStarter (com.android.server.wm)
startActivityAsUser:1145, ActivityTaskManagerService (com.android.server.wm)
startActivityAsUser:1117, ActivityTaskManagerService (com.android.server.wm)
startActivity:1092, ActivityTaskManagerService (com.android.server.wm)
onTransact:1429, IActivityTaskManager$Stub (android.app)
execTransactInternal:1154, Binder (android.os)
execTransact:1123, Binder (android.os)<\/p>\n<\/blockquote>\n\n
realStartActivityLocked:721, ActivityStackSupervisor (com.android.server.wm) startSpecificActivity:982, ActivityStackSupervisor (com.android.server.wm) resumeTopActivityInnerLocked:1958, ActivityStack (com.android.server.wm) resumeTopActivityUncheckedLocked:1507, ActivityStack (com.android.server.wm) resumeFocusedStacksTopActivities:2362, RootWindowContainer (com.android.server.wm) completePauseLocked:1209, ActivityStack (com.android.server.wm) activityPaused:4973, ActivityRecord (com.android.server.wm) activityPaused:1890, ActivityTaskManagerService (com.android.server.wm) onTransact:1951, IActivityTaskManager$Stub (android.app) execTransactInternal:1154, Binder (android.os) execTransact:1123, Binder (android.os) <\/pre>\n<\/blockquote>\n
framework\u901a\u77e5activity\u751f\u547d\u5468\u671f-ActivityLifecycleItem<\/h4>\n
StopActivityItem
DestroyActivityItem
ResumeActivityItem
PauseActivityItem<\/p>\n
ConfigurationChangeItem
EnterPipRequestedItem
FixedRotationAdjustmentsItem
ActivityResultItem
ActivityConfigurationChangeItem
MoveToDisplayItem
NewIntentItem
TopResumedActivityChangeItem
ActivityRelaunchItem
LaunchActivityItem<\/p>\n\n
final ActivityLifecycleItem lifecycleItem; \/\/\u521b\u5efaResumeActivityItem lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward()); \/\/\u521b\u5efaClientTeansaction, \u5e76\u8bbe\u7f6elifecycleItem final ClientTransaction clientTransaction = ClientTransaction.obtain( proc.getThread(), r.appToken); clientTransaction.setLifecycleStateRequest(lifecycleItem); \/\/ Schedule transaction. mService.getLifecycleManager\u4e3aClientLifecycleManager mService.getLifecycleManager().scheduleTransaction(clientTransaction); \u4e86<\/pre>\n<\/blockquote>\n
\u7c7b\u56fe<\/h4>\n
\u6d41\u7a0b\u56fe<\/h4>\n
\u53c2\u8003\u6587\u6863<\/h3>\n
\n
\n