日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 四大组件之——Acitivity(三) 深入了解Activity的启动流程

發布時間:2025/4/16 Android 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 四大组件之——Acitivity(三) 深入了解Activity的启动流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上圖為整個Activity的啟動流程

接下來我們大概分析

?在我們的Android系統中,應用程序是由Launcher這個應用啟動起來的。當我們安裝好應用程序之后,就會在Launcher的界面上生成一個圖標,我們點擊圖標時Launch就會啟動我們的應用程序。

1.點擊圖標,launcher調用onClick方法

/*** * * @param v The view representing the clicked shortcut. 1.此處的view是指被點擊的桌面圖標*/public void onClick(View v) {// Make sure that rogue clicks don't get through while allapps is// launching, or after the// view has detached (it's possible for this to happen if the view is// removed mid touch).if (v.getWindowToken() == null) {return;}if (!mWorkspace.isFinishedSwitchingState()) {return;}Object tag = v.getTag();if (tag instanceof ShortcutInfo) {// 打開快捷方式對應的intentfinal Intent intent = ((ShortcutInfo) tag).intent;int[] pos = new int[2];v.getLocationOnScreen(pos);intent.setSourceBounds(new Rect(pos[0], pos[1], pos[0]+ v.getWidth(), pos[1] + v.getHeight()));boolean success = startActivitySafely(v, intent, tag);//2.同時調用startActivitySafelyif (success && v instanceof BubbleTextView) {mWaitingForResume = (BubbleTextView) v;mWaitingForResume.setStayPressed(true);}} else if (tag instanceof FolderInfo) {if (v instanceof FolderIcon) {// 打開文件夾FolderIcon fi = (FolderIcon) v;handleFolderClick(fi);}} else if (v == mAllAppsButton) {// 顯示或者不顯示“全部程序”界面if (isAllAppsVisible()) {showWorkspace(true);} else {onClickAllAppsButton(v);}}}


2.在launcher的onClick方法里調用了startActivitySafely()方法,見上述代碼

boolean startActivitySafely(View v, Intent intent, Object tag) {boolean success = false;try {success = startActivity(v, intent, tag); 由此可見StartActivitySafely(),最終是調用startActivity()方法} catch (ActivityNotFoundException e) {Toast.makeText(this, R.string.activity_not_found,Toast.LENGTH_SHORT).show();Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);}return success;} boolean startActivity(View v, Intent intent, Object tag) {intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);try {// Only launch using the new animation if the shortcut has not opted// out (this is a// private contract between launcher and may be ignored in the// future).boolean useLaunchAnimation = (v != null)&& !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);if (useLaunchAnimation) {ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v,0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());startActivity(intent, opts.toBundle());} else {startActivity(intent);}return true;} catch (SecurityException e) {Toast.makeText(this, R.string.activity_not_found,Toast.LENGTH_SHORT).show();Log.e(TAG,"Launcher does not have the permission to launch "+ intent+ ". Make sure to create a MAIN intent-filter for the corresponding activity "+ "or use the exported attribute for this activity. "+ "tag=" + tag + " intent=" + intent, e);}return false;}

3.在startActivity()方法中,intent 添加了flag FLAG_ACTIVITY_NEW_TASK , 此標志為創建新的任務棧,在創建目標 任務棧之前,首先會調用ActivityThread類啟動launcher的進程,然后啟動目標應用的任務棧,啟動完成之后,目標應用的任務棧會通知 ActivityThread調用launcher的onPause方法。此時,ActivityThread啟動新的應用進程(也就是目標應用的進程)。
4.目標應用的進程通過loadClass加載MainActivity,然后通過H(handler)來控制MainActivity的生命周期




總結

以上是生活随笔為你收集整理的Android 四大组件之——Acitivity(三) 深入了解Activity的启动流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。