Android:onNewIntent()觸發機制及注意事項

來源:互聯網
上載者:User

標籤:

一、onNewIntent()

在IntentActivity中重寫下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestroy  onNewIntent


1、其他應用發Intent,執行下列方法:
onCreate
onStart
onResume

發Intent的方法:

1 Uri uri = Uri.parse("philn://blog.163.com");
2 Intent it = new Intent(Intent.ACTION_VIEW, uri);   
3 startActivity(it);

2、接收Intent聲明:

1 <activity android:name=".IntentActivity" android:launchMode="singleTask"
2                   android:label="@string/testname">
3              <intent-filter>
4                 <action android:name="android.intent.action.VIEW" />
5                 <category android:name="android.intent.category.DEFAULT" />
6                 <category android:name="android.intent.category.BROWSABLE" />
7                 <data android:scheme="philn"/>
8             </intent-filter>
9 </activity>

3、如果IntentActivity處於任務棧的頂端,也就是說之前開啟過的Activity,現在處於onPause、onStop狀態的話,其他應用再發送Intent的話,執行順序為:
onNewIntent,onRestart,onStart,onResume。

在Android應用程式開發的時候,從一個Activity啟動另一個Activity並傳遞一些資料到新的Activity上非常簡單,但是當您需要讓後台啟動並執行Activity回到前台並傳遞一些資料可能就會存在一點點小問題。

首先,在預設情況下,當您通過Intent啟到一個Activity的時候,就算已經存在一個相同的正在啟動並執行Activity,系統都會建立一個新的Activity執行個體並顯示出來。為了不讓Activity執行個體化多次,我們需要通過在AndroidManifest.xml配置activity的載入方式(launchMode)以實現單任務模式,如下所示:

1 <activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"></activity>


launchMode為singleTask的時候,通過Intent啟到一個Activity,如果系統已經存在一個執行個體,系統就會將請求發送到這個執行個體上,但這個時候,系統就不會再調用通常情況下我們處理請求資料的onCreate方法,而是調用onNewIntent方法,如下所示:

1 protected void onNewIntent(Intent intent) {
2  
3   super.onNewIntent(intent);
4  
5   setIntent(intent);//must store the new intent unless getIntent() will return the old one
6  
7   processExtraData();
8  
9 }


不要忘記,系統可能會隨時殺掉後台啟動並執行 Activity ,如果這一切發生,那麼系統就會調用 onCreate 方法,而不調用 onNewIntent 方法,一個好的解決方案就是在 onCreate 和 onNewIntent 方法中調用同一個處理資料的方法,如下所示:

1 public void onCreate(Bundle savedInstanceState) {
2  
3   super.onCreate(savedInstanceState);
4  
5   setContentView(R.layout.main);
6  
7   processExtraData();
8  
9 }
1 protected void onNewIntent(Intent intent) {
2  
3   super.onNewIntent(intent);
4  
5    setIntent(intent);//must store the new intent unless getIntent() will return the old one
6  
7   processExtraData()
8  
9 }
1 private void processExtraData(){
2  
3   Intent intent = getIntent();
4  
5   //use the data received here
6  
7 }

 

二、onNewIntent()的setIntent()和getIntent()

1 @Override
2 protected void onNewIntent(Intent intent) {
3     super.onNewIntent(intent);
4  
5     // setIntent(intent);
6  
7     int data = getIntent().getIntExtra("HAHA"0);
8     // int data = intent.getIntExtra("HAHA", 0);
9 }

如果沒有調用setIntent(intent),則getIntent()擷取的資料將不是你所期望的。但是使用intent.getInXxx,貌似可以獲得正確的結果。

注意這句話:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

所以最好是調用setIntent(intent),這樣在使用getIntent()的時候就不會有問題了。

Android:onNewIntent()觸發機制及注意事項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.