在IntentActivity中重寫下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent
1、其餘應用發Intent,執行下列方法:
onCreate
onStart
onResumeandroid
發Intent的方法:app
Uri uri = Uri.parse("philn://blog.163.com"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
2、接收Intent聲明:less
<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="philn"/> </intent-filter> </activity>
3、若是IntentActivity處於任務棧的頂端,也就是說以前打開過的Activity,如今處於onPause、onStop 狀態的話,其餘應用再發送Intent的話,執行順序爲:
onNewIntent onRestart onStart onResume。xml
在Android應用程序開發的時候,從一個Activity啓動另外一個Activity並傳遞一些數據到新的Activity上很是簡單,可是當您須要讓後臺運行的Activity回到前臺並傳遞一些數據可能就會存在一點點小問題。blog
首先,在默認狀況下,當您經過Intent啓到一個Activity的時候,就算已經存在一個相同的正在運行的Activity,系統都會建立一個新的Activity實例並顯示出來。爲了避免讓Activity實例化屢次,咱們須要經過在AndroidManifest.xml配置activity的加載方式(launchMode)以實現單任務模式,以下所示:開發
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1">get
</activity>string
launchMode爲singleTask的時候,經過Intent啓到一個Activity,若是系統已經存在一個實例,系統就會將請求發送到這個實例上,但這個時候,系統就不會再調用一般狀況下咱們處理請求數據的onCreate方法,而是調用onNewIntent方法,以下所示:it
protected void onNewIntent(Intent intent) {io
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
}