日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android應(yīng)用開發(fā)教程:兩個運行的Activity之間的通信

在Android應(yīng)用程序開發(fā)的時候,從一個activity啟動另一個Activity并傳遞一些數(shù)據(jù)到新的Activity上非常簡單,但是當您需要讓后臺運行的Activity回到前臺并傳遞一些數(shù)據(jù)可能就會存在一點點小問題。

創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供網(wǎng)站設(shè)計、網(wǎng)站建設(shè)網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計,成都響應(yīng)式網(wǎng)站建設(shè)公司,網(wǎng)頁設(shè)計師打造企業(yè)風格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:18982081108

首先,在默認情況下,當您通過Intent啟到一個Activity的時候,就算已經(jīng)存在一個相同的正在運行的Activity,系統(tǒng)都會創(chuàng)建一個新的Activity實例并顯示出來。為了不讓Activity實例化多次,我們需要通過在AndroidManifest.xml配置activity的加載方式(launchMode)以實現(xiàn)單任務(wù)模式,如下所示:

1"@string/app_name" android:launchmode="singleTask"android:name="Activity1">
2

launchMode為singleTask的時候,通過Intent啟到一個Activity,如果系統(tǒng)已經(jīng)存在一個實例,系統(tǒng)就會將請求發(fā)送到這個實例上,但這個時候,系統(tǒng)就不會再調(diào)用通常情況下我們處理請求數(shù)據(jù)的onCreate方法,而是調(diào)用onNewIntent方法,如下所示:

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

不要忘記,系統(tǒng)可能會隨時殺掉后臺運行的Activity,如果這一切發(fā)生,那么系統(tǒng)就會調(diào)用onCreate方法,而不調(diào)用onNewIntent方法,一個好的解決方法就是在onCreate和onNewIntent方法中調(diào)用同一個處理數(shù)據(jù)的方法,如下所示:

01public void onCreate(Bundle savedInstanceState) {
02  super.onCreate(savedInstanceState);
03  setContentView(R.layout.main);
04  processExtraData();
05}
06 
07protected void onNewIntent(Intent intent) {
08  super.onNewIntent(intent);
09  setIntent(intent);//must store the new intent unless getIntent() will return the old one
10  processExtraData()
11}
12 
13private void processExtraData(){
14  Intent intent = getIntent();
15  //use the data received here
16}

【編輯推薦】

  1. Android學習筆記:Activity跳轉(zhuǎn)
  2. Intent,Android應(yīng)用核心探秘
  3. 闡述Android Intent使用整理問題
  4. Android Activity進行全解析
  5. Android Activity跳轉(zhuǎn)相關(guān)操作技巧分享

當前名稱:Android應(yīng)用開發(fā)教程:兩個運行的Activity之間的通信
轉(zhuǎn)載源于:http://www.dlmjj.cn/article/cogocgi.html