在IntentActivity中重寫下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent一、其他應用發Intent,執行下列方法:I/@@@philn(12410): onCreateI/@@@philn(12410): onStartI/@@@philn(12410): onResume 發Intent的方法:Uri uri = Uri.parse("http://www.cnblogs.com/rayray/");Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); 二、接收Intent聲明:<activity android:name=".IntentActivity" android:launchMode="singleTask"android:label="@string/testname"><intent-filter><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.BROWSABLE" /><data android:scheme="http"/></intent-filter></activity> 三、如果IntentActivity處於任務棧的頂端,也就是說之前開啟過的Activity,現在處於I/@@@philn(12410): onPauseI/@@@philn(12410): onStop 狀態的話其他應用再發送Intent的話,執行順序為:I/@@@philn(12410): onNewIntentI/@@@philn(12410): onRestartI/@@@philn(12410): onStartI/@@@philn(12410): onResume 在Android應用程式開發的時候,從一個Activity啟動另一個Activity並傳遞一些資料到新的Activity上非常簡單,但是當您需要讓後台啟動並執行Activity回到前台並傳遞一些資料可能就會存在一點點小問題。 首先,在預設情況下,當您通過Intent啟到一個Activity的時候,就算已經存在一個相同的正在啟動並執行Activity,系統都會建立一個新的Activity執行個體並顯示出來。為了不讓Activity執行個體化多次,我們需要通過在AndroidManifest.xml配置activity的載入方式(launchMode)以實現單任務模式,如下所示: <activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity> launchMode為singleTask的時候,通過Intent啟到一個Activity,如果系統已經存在一個執行個體,系統就會將請求發送到這個執行個體上,但這個時候,系統就不會再調用通常情況下我們處理請求資料的onCreate方法,而是調用onNewIntent方法,如下所示: 複製代碼protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);//must store the new intent unless getIntent() will return the old one processExtraData(); }複製代碼 不要忘記,系統可能會隨時殺掉後台啟動並執行Activity,如果這一切發生,那麼系統就會調用onCreate方法,而不調用onNewIntent方法,一個好的解決方案就是在onCreate和onNewIntent方法中調用同一個處理資料的方法,如下所示: 複製代碼public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); processExtraData(); } protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);//must store the new intent unless getIntent() will return the old one processExtraData() } private void processExtraData(){ Intent intent = getIntent(); //use the data received here }