最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android onNewIntent()觸發(fā)機(jī)制及注意事項(xiàng)

 更新時(shí)間:2016年09月15日 08:59:29   作者:Xushao  
這篇文章主要介紹了Android onNewIntent()觸發(fā)機(jī)制及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下

一、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)文章

最新評(píng)論

沛县| 海阳市| 通化市| 望都县| 义马市| 砚山县| 霍山县| 新安县| 尤溪县| 休宁县| 芮城县| 台东县| 普陀区| 吉安市| 方山县| 金川县| 来凤县| 清河县| 阿瓦提县| 新密市| 临西县| 海丰县| 靖江市| 万荣县| 宁远县| 哈尔滨市| 卢龙县| 翁源县| 中西区| 新乡县| 平山县| 玛纳斯县| 吉林市| 儋州市| 当雄县| 政和县| 郴州市| 醴陵市| 永川市| 乌恰县| 平定县|