Android onNewIntent()觸發(fā)機制及注意事項
一、onNewIntent()
在IntentActivity中重寫下列方法:onCreate onStart onRestart onResume onPause onStop onDestroy onNewIntent
1、其他應(yīng)用發(fā)Intent,執(zhí)行下列方法:
onCreate
onStart
onResume
發(fā)Intent的方法:
Uri uri = Uri.parse("philn://blog.163.com"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
2、接收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="philn"/> </intent-filter> </activity>
3、如果IntentActivity處于任務(wù)棧的頂端,也就是說之前打開過的Activity,現(xiàn)在處于onPause、onStop 狀態(tài)的話,其他應(yīng)用再發(fā)送Intent的話,執(zhí)行順序為:
onNewIntent,onRestart,onStart,onResume。
在Android應(yīng)用程序開發(fā)的時候,從一個Activity啟動另一個Activity并傳遞一些數(shù)據(jù)到新的Activity上非常簡單,但是當(dāng)您需要讓后臺運行的Activity回到前臺并傳遞一些數(shù)據(jù)可能就會存在一點點小問題。
首先,在默認(rèn)情況下,當(dāng)您通過Intent啟到一個Activity的時候,就算已經(jīng)存在一個相同的正在運行的Activity,系統(tǒng)都會創(chuàng)建一個新的Activity實例并顯示出來。為了不讓Activity實例化多次,我們需要通過在AndroidManifest.xml配置activity的加載方式(launchMode)以實現(xiàn)單任務(wù)模式,如下所示:
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity>
launchMode為singleTask的時候,通過Intent啟到一個Activity,如果系統(tǒng)已經(jīng)存在一個實例,系統(tǒng)就會將請求發(fā)送到這個實例上,但這個時候,系統(tǒng)就不會再調(diào)用通常情況下我們處理請求數(shù)據(jù)的onCreate方法,而是調(diào)用onNewIntent方法,如下所示:
protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent);//must store the new intent unless getIntent() will return the old one processExtraData(); }
不要忘記,系統(tǒng)可能會隨時殺掉后臺運行的 Activity ,如果這一切發(fā)生,那么系統(tǒng)就會調(diào)用 onCreate 方法,而不調(diào)用 onNewIntent 方法,一個好的解決方法就是在 onCreate 和 onNewIntent 方法中調(diào)用同一個處理數(shù)據(jù)的方法,如下所示:
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 }
二、onNewIntent()的setIntent()和getIntent()
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // setIntent(intent); int data = getIntent().getIntExtra("HAHA", 0); // int data = intent.getIntExtra("HAHA", 0); }
如果沒有調(diào)用setIntent(intent),則getIntent()獲取的數(shù)據(jù)將不是你所期望的。但是使用intent.getInXxx,貌似可以獲得正確的結(jié)果。
注意這句話:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
所以最好是調(diào)用setIntent(intent),這樣在使用getIntent()的時候就不會有問題了。
以上就是對Android onNewIntent()觸發(fā)機制及注意事項 的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
Kotlin實現(xiàn)Android系統(tǒng)懸浮窗詳解
大家好,本篇文章主要講的是Kotlin實現(xiàn)Android系統(tǒng)懸浮窗詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android Activity啟動模式之singleTask實例詳解
這篇文章主要介紹了Android Activity啟動模式之singleTask,結(jié)合實例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-01-01Android開發(fā)實戰(zhàn)之漂亮的ViewPager引導(dǎo)頁
這篇文章主要介紹了Android開發(fā)實戰(zhàn)中漂亮ViewPager引導(dǎo)頁的制作過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Android在listview添加checkbox實現(xiàn)原理與代碼
Android在listview添加checkbox如何實現(xiàn)一直都是新手朋友們的頭疼問題,接下來為您詳細(xì)介紹實現(xiàn)方法,感興趣的朋友可以了解下2013-01-01Android高手進階教程(二十六)之---Android超仿Path菜單的功能實現(xiàn)!
本篇文章主要主要介紹了Android超仿Path菜單的功能實現(xiàn),現(xiàn)在分享給大家,也給大家做個參考。感興趣的可以了解一下。2016-11-11