編輯:關於 Android 程式設計
一、onNewIntent()
在 IntentActivity 中重寫下列方法:onCreate、onStart、onRestart、onResume、onPause、onStop、onDestroy、onNewIntent。
1. 其他應用發送 Intent 時,執行下列方法:
onCreate → onStart → onResume。
發 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 處於任務堆疊頂端(即之前打開過、現處於 onPause、onStop 狀態),其他應用再發送 Intent 時,執行順序為:onNewIntent → onRestart → onStart → onResume。
在 Android 開發中,從一個 Activity 啟動另一個 Activity 並傳遞數據給新 Activity 非常簡單,但當你需要讓後台運行的 Activity 回到前台並傳遞數據時,可能就會遇到一點小問題。
首先,預設情況下,當你透過 Intent 啟動一個 Activity 時,即便已存在一個相同的、正在運行的 Activity,系統仍會建立一個新的 Activity 實例並顯示出來。為避免 Activity 被多次實例化,我們需要在 AndroidManifest.xml 中配置 Activity 的加載方式(launchMode)以實現單任務模式:
<activity android:label="@string/app_name" android:launchMode="singleTask" android:name="Activity1"></activity>
當 launchMode 為 singleTask 時,透過 Intent 啟動一個 Activity,若系統已存在一個實例,系統會把請求發送到這個實例,但此時不會再呼叫通常處理請求數據的 onCreate,而是呼叫 onNewIntent:
protected void onNewIntent(Intent intent) { 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); 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); }
如果沒有呼叫 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() 觸發機制及注意事項的整理,謝謝大家對本站的支持!
使用 Eclipse 建置 Android 開發環境
早期 Android 開發常以 Eclipse 搭配 ADT 插件完成。流程大致如下。下載工具先準備 Eclipse、JDK 與 Android SDK。Eclipse
Activity、Window 與 View:Android 界面繪製全鏈路拆解
本文想回答一個問題:Activity 到底是怎麼透過 Window 與 View 把自己畫到螢幕上的。 先看 Lifecycle 這裡只聚焦兩個最常混淆的場景——按 H
SharedPreferences 用法詳解:輕量級參數存儲與跨應用訪問
很多應用都需要提供參數設置功能,比如 QQ 裡「是否允許陌生人加我為好友」。保存這類配置參數,Windows 軟體通常用 ini 檔,J2SE 應用常用 properti
Android 學習筆記(五):Service 全面解析——從分類、生命週期到 Binder 跨進程通信
一、Service 到底是什麼? Service 是 Android 四大組件之一,官方文件對它的定義是: A Service is an application co