Android onNewIntent()觸發(fā)機(jī)制及注意事項(xiàng)
一、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í)行順序?yàn)椋?br />
onNewIntent,onRestart,onStart,onResume。
在Android應(yīng)用程序開發(fā)的時(shí)候,從一個(gè)Activity啟動(dòng)另一個(gè)Activity并傳遞一些數(shù)據(jù)到新的Activity上非常簡(jiǎn)單,但是當(dāng)您需要讓后臺(tái)運(yùn)行的Activity回到前臺(tái)并傳遞一些數(shù)據(jù)可能就會(huì)存在一點(diǎn)點(diǎn)小問題。
首先,在默認(rèn)情況下,當(dāng)您通過Intent啟到一個(gè)Activity的時(shí)候,就算已經(jīng)存在一個(gè)相同的正在運(yùn)行的Activity,系統(tǒng)都會(huì)創(chuàng)建一個(gè)新的Activity實(shí)例并顯示出來。為了不讓Activity實(shí)例化多次,我們需要通過在AndroidManifest.xml配置activity的加載方式(launchMode)以實(shí)現(xiàn)單任務(wù)模式,如下所示:
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity>
launchMode為singleTask的時(shí)候,通過Intent啟到一個(gè)Activity,如果系統(tǒng)已經(jīng)存在一個(gè)實(shí)例,系統(tǒng)就會(huì)將請(qǐng)求發(fā)送到這個(gè)實(shí)例上,但這個(gè)時(shí)候,系統(tǒng)就不會(huì)再調(diào)用通常情況下我們處理請(qǐng)求數(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)可能會(huì)隨時(shí)殺掉后臺(tái)運(yùn)行的 Activity ,如果這一切發(fā)生,那么系統(tǒng)就會(huì)調(diào)用 onCreate 方法,而不調(diào)用 onNewIntent 方法,一個(gè)好的解決方法就是在 onCreate 和 onNewIntent 方法中調(diào)用同一個(gè)處理數(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()的時(shí)候就不會(huì)有問題了。
以上就是對(duì)Android onNewIntent()觸發(fā)機(jī)制及注意事項(xiàng) 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
本篇文章主要介紹了android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Android開發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法,涉及Android針對(duì)Excel數(shù)據(jù)讀取及xml格式文件的構(gòu)造與保存相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android控件之RatingBar自定義星級(jí)評(píng)分樣式
RatingBar為評(píng)分條控件,默認(rèn)效果為若干個(gè)綠色的星星,如果想將其換成其他自定義圖片就要自定義它的style。接下來通過本文給大家介紹Android控件之RatingBar自定義星級(jí)評(píng)分樣式,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Android Activity啟動(dòng)模式之singleTop實(shí)例詳解
這篇文章主要介紹了Android Activity啟動(dòng)模式之singleTop,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTop模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01
Android開發(fā)實(shí)現(xiàn)AlertDialog中View的控件設(shè)置監(jiān)聽功能分析
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)AlertDialog中View的控件設(shè)置監(jiān)聽功能,結(jié)合實(shí)例形式分析了Android針對(duì)AlertDialog中的控件使用View進(jìn)行監(jiān)聽的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例
最近看到豆瓣FM的音樂播放界面,有一個(gè)環(huán)形的進(jìn)度條挺不錯(cuò)的,最近有空就想著實(shí)現(xiàn)了,所以下面這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04

